我在这里使用2d数组,我不太习惯。但是我不认为我在这里做错了什么。
#include <stdio.h>
#include <math.h>
int square(int x)
{
return x*x;
}
int main()
{
int t; //number of testcases
scanf("%d", &t);
while (t--)
{
int n, m, cnt=1;
scanf("%d %d", &n, &m);
printf("Case %d:\n", cnt);
cnt++;
int arr[n][2]; //the 2d array with the entire record of the positions of the houses in village
for (int i=0; i<n; i++)
{
scanf("%d %d", &arr[i][0], &arr[i][1]);
}
for (int j=0; j<m; j++)
{
int h, x, y, total=0;
scanf("%d %d %d", &h, &x, &y);
arr[h-1][0]=x;
arr[h-1][1]=y;
for (int k=0; k<(n-1); k++)
{
total+=square((arr[k][0])-arr[k+1][0])); //get error "expected ';' before token )"
total+=square((arr[k][1])-arr[k+1][1]));
}
total+=square((arr[n-1][0])-arr[0][0]));
total+=square((arr[n-1][1])-arr[0][1]));
printf("%d\n", total);
}
}
return 0;
}
我想出了解决此问题的方法,下面我将尝试简要描述一下
山姆在他的村庄分发报纸。该村庄有n座房屋,编号从1到n,可以按笛卡尔点(x,y)的形式进行识别。每天早上,Sam开始从1号房分发报纸,然后按照门数增加的顺序转移到其他房屋。在旅途的最后,他回到了1号房。
在两次旅行之间,他一直跟踪行进该距离要花费多少钱。从一所房子到另一所房子的旅行成本是它们之间的欧式距离的平方。使事情变得复杂的是,房屋不断地从一个地方移动到另一个地方,并且位置不断变化。我们的工作是计算与房屋的最新位置相关的整个旅程所需的总费用。
现在我们将得到房屋数量n和查询数量m。输入将有n行,然后在每行中,我们将具有该门牌号的相应x和y坐标。随后还有m行,每行都是一个查询,其中包含更改位置的房屋编号以及该特定房屋的新坐标。
对于每个查询,我们都需要打印山姆然后围绕村屋进行一轮调查所需的总费用。
(Dev C ++)编译器在实现square()函数的每一行上都给我此错误消息:
预期的';'在')'标记之前
最佳答案
您应该从这些行中删除一个多余的括号(')'
)
total+=square((arr[k][0])-arr[k+1][0]));
total+=square((arr[k][1])-arr[k+1][1]));
total+=square((arr[n-1][0])-arr[0][0]));
total+=square((arr[n-1][1])-arr[0][1]));
并请仔细检查您的
while
条件。#include <stdio.h>
#include <math.h>
int square(int x)
{
return x*x;
}
int main()
{
int t; //number of testcases
scanf("%d", &t);
while (t--)
{
int n, m, cnt=1;
scanf("%d %d", &n, &m);
printf("Case %d:\n", cnt);
cnt++;
int arr[n][2]; //the 2d array with the entire record of the positions of the houses in village
for (int i=0; i<n; i++)
{
scanf("%d %d", &arr[i][0], &arr[i][1]);
}
for (int j=0; j<m; j++)
{
int h, x, y, total=0;
scanf("%d %d %d", &h, &x, &y);
arr[h-1][0]=x;
arr[h-1][1]=y;
for (int k=0; k<(n-1); k++)
{
total+=square((arr[k][0])-arr[k+1][0]); //removed extra ')'
total+=square((arr[k][1])-arr[k+1][1]); //removed extra ')'
}
total+=square((arr[n-1][0])-arr[0][0]); //removed extra ')'
total+=square((arr[n-1][1])-arr[0][1]); //removed extra ')'
printf("%d\n", total);
}
}
return 0;
}