从 C 文件中读取测试用例时出错

发布于 2024-11-19 15:26:27 字数 1139 浏览 5 评论 0原文

对于一个编程作业,我正在实现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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-11-26 15:26:28

您在循环内修改了循环变量i,这通常是不需要的。在本例中,由于 i 循环到 11(边数),因此导致程序终止,因为 11 不小于 2(输入中的测试用例数)。

您可以使用临时变量(如果您使用的是 C++,谢谢 aardvarkk):

for (int i=0; i<testCases; ++i)
    {
        scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
        for (int j=0; j<numberEdges; j++)

请注意,int j 也可以是 int i,但我不推荐它。只需使用具有其他名称的变量会更清晰。或者,如果您使用 C,只需删除两个 int 并使用函数的本地变量即可。

有关更多信息,您可以阅读

You have the loop variable i modified inside the loop, which is generally unwanted. In this case, since i 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):

for (int i=0; i<testCases; ++i)
    {
        scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
        for (int j=0; j<numberEdges; j++)

Note that the int j could also be int 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 two int and use variables that are local to the function.

For more, you could read this.

你是年少的欢喜 2024-11-26 15:26:28

您的代码在我的机器上产生了以下输出。我所做的唯一更改是在调用 freopen 之前声明 intij 等代码标准C。

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
Caso 1: Total Weight 0

它肯定比你的测试用例阅读更多,所以我不确定问题是什么?

Your code produced the following output on my machine. The only change I made was to declare the int values i, j etc. before the freopen call to make the code standard C.

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
Caso 1: Total Weight 0

It's definitely reading more than your test cases, so I'm not sure what the problem is?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文