从 C 文件中读取测试用例时出错
对于一个编程作业,我正在实现Prim算法,测试用例的输入文件的格式如下:
输入的第一行是一个整数C,它表示测试用例的数量。每个测试用例的第一行包含两个整数N和E,其中N分别表示图中的节点数,E表示边数。然后是 E 行,每行有 3 个整数 I、J 和 P,其中 I 和 J 表示一条边的节点(无向图,其中 0 ≤ I, J
尽管我正在开始编写代码(我是编程新手),但我不明白为什么我的代码只读取测试用例的条目,我做错了什么?
这是读取文件 entradaA.in 的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv []){
int testCases;
int numberNodes;
int numberEdges;
freopen("entradaA.in", "r", stdin);
int i, j, cont=1;
int total = 0;
int a, b, c;
scanf ("%d", &testCases);
for (i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (i=0; i<numberEdges; i++)
{
scanf ("%d %d %d", &a,&b,&c);//
printf ("%d %d %d\n", a, b, c);
}
printf ("Caso %d: Total Weight %d\n", cont++, total);
}
return (0);
}
输入文件 (entradaA.in) 看起来像这样
2
7 11
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
6 9
0 1 30
0 2 30
1 3 22
1 5 33
2 3 20
2 4 33
3 4 15
3 5 20
5 4 20
for a programming homework, I'm implementing Prim's algorithm, the format of the input file for test cases is as follows:
The first line of input will be an integer C, which indicates the number of test cases. The first line of each test case contains two integers N and E, where N represents the number of nodes in the graph and E the number of edges, respectively. Then come E lines, each with 3 integers I, J and P, where I and J represent the nodes of an edge (undirected graphs, where 0 ≤ I, J
Although even I'm starting the code (I'm new to programming) i Don´t understand why my code only reads an entry for the test cases, What am I doing wrong?
this is the code reading the file entradaA.in:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv []){
int testCases;
int numberNodes;
int numberEdges;
freopen("entradaA.in", "r", stdin);
int i, j, cont=1;
int total = 0;
int a, b, c;
scanf ("%d", &testCases);
for (i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (i=0; i<numberEdges; i++)
{
scanf ("%d %d %d", &a,&b,&c);//
printf ("%d %d %d\n", a, b, c);
}
printf ("Caso %d: Total Weight %d\n", cont++, total);
}
return (0);
}
The input file (entradaA.in) look something like this
2
7 11
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
6 9
0 1 30
0 2 30
1 3 22
1 5 33
2 3 20
2 4 33
3 4 15
3 5 20
5 4 20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在循环内修改了循环变量
i
,这通常是不需要的。在本例中,由于i
循环到 11(边数),因此导致程序终止,因为 11 不小于 2(输入中的测试用例数)。您可以使用临时变量(如果您使用的是 C++,谢谢 aardvarkk):
请注意,
int j
也可以是int i
,但我不推荐它。只需使用具有其他名称的变量会更清晰。或者,如果您使用 C,只需删除两个 int 并使用函数的本地变量即可。有关更多信息,您可以阅读此。
You have the loop variable
i
modified inside the loop, which is generally unwanted. In this case, sincei
looped to 11 (the number of edges), it caused your program to terminate since 11 is not smaller than 2 (the number of test cases in the input).You could use temporary variable (if you are using C++, thank you aardvarkk):
Note that the
int j
could also beint i
, but I wouldn't recommend it. Just use a variable with another name would be clearer. Or if you are in C, just drop the twoint
and use variables that are local to the function.For more, you could read this.
您的代码在我的机器上产生了以下输出。我所做的唯一更改是在调用
freopen
之前声明int
值i
、j
等代码标准C。它肯定比你的测试用例阅读更多,所以我不确定问题是什么?
Your code produced the following output on my machine. The only change I made was to declare the
int
valuesi
,j
etc. before thefreopen
call to make the code standard C.It's definitely reading more than your test cases, so I'm not sure what the problem is?