计算两点间的距离
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 415904 Accepted Submission(s): 139184
#include<stdio.h>
#include<math.h>
int main() {
float X1, Y1, X2, Y2;
while (scanf("%f %f %f %f", &X1, &Y1, &X2, &Y2) != EOF) {
float y = fabs(Y2 - Y1);
float x = fabs(X2 - X1);
printf("%.2f\n", sqrt(x * x + y * y));
}
return 0;
}
注意:
#include<math.h> //头文件(数学)
sqrt(); //平方函数
fabs(); //绝对值函数
printf("%.2f",a); //a为实数类型,保留两位小数
printf("%.3f",a); //a为实数类型,保留三位小数
/*数据类型与输出格式的关系*/
------------------------------------------------
int a;
printf("%d",a);
long a; //long int a;
printf("%ld",a);
long long a; //long long int a;
printf("%lld",a);
-------------------------------------------------
float a;
printf("%f",a);
double a;
printf("%lf",a);
-------------------------------------------------
char a;
printf("%c",a);
char a[100]; //字符串
printf("%s",a); //输出到'\0'
printf("%c",a); //输出a[0]
思想:
- 距离公式:$${\rm{d}} = \sqrt {{{(x1 - x2)}^2} + {{(y1 - y2)}^2}}$$