结构体初步,我应该是后面算走过的总路程错了
题目:平面坐标系中有一只蚂蚁,它从第1个点出发,按照直线爬行到第2个点,再从第2个点按照直线爬行到第3个点,……,直到爬到最后一个点。请你编程算出蚂蚁的总的爬行距离输入
第1行是一个整数,表示点的个数
接下来是n行,每行2个整数,表示n个点的坐标。
输出
蚂蚁的总爬行距离
样例输入
3
0 0
0 3
4 0
样例输出
8.00
程序代码:#include <stdio.h>
#include <math.h>
struct POINT
{
int x;
int y;
};
int N;//点的个数
void getpoint(struct POINT *p,int num);
float getdistance(struct POINT *p,int num);
main()
{ int i;
struct POINT point[60];
float distance;
scanf("%d",&N);
getpoint(point,N);
distance=getdistance(point,N);
printf("%.2f",distance);
}
void getpoint(struct POINT *p,int num)
{ int i;
for(i=0;i<num;i++)
{
scanf("%f %f",&p[i].x,&p[i].y);
}
}
float getdistance(struct POINT *p,int num)
{
int i;
float sum=0;
for(i=1;i<num+1;i++)
{
sum=sum+sqrt((p[i].x-p[i-1].x)*(p[i].x-p[i-1].x)+(p[i].y-p[i-1].y)*(p[i].y-p[i-1].y));
}
return sum;
}



