c - 输出不正确: determine if three digits are unique and add up to a given sum

标签 c

The Problem:

Given a goal sum and three positive digits, determine if the three positive digits are unique and sum up to the goal.

The Input:

Input will begin with a single, positive integer, n, on a line by itself. On the next n lines will be a single positive integer representing the goal sum followed by three single positive digits, each separated by a single space.

The Output:

For each line of input, determine if the three digits sum to the goal and are unique. Output “Proper triplet” on a line by itself if so, or “Not a good triplet” on a line by itself if not.

int main()
{
   FILE *ifp;

   //Open file
   ifp = fopen("kakuro.in", "r");

   int numcases, index;

   fscanf(ifp, "%d", &numcases);

   //Go through each case
   for (index=0; index<numcases; index++){

    int target,n1,n2,n3, total;

    //Read in data and allocate to variables above

    fscanf(ifp, "%d%d%d%d", target,n1,n2,n3);
    total = n1+n2+n3;

    //add numbers together
    if (total == target){
        //check for duplicate numbers
        if ( n1 != n2 && n2 != n3 &&n1 != n3)
            printf("numbers pass test\n");
        else printf("numbers are repeated,test failed\n");
    }
    else {printf ("The total does not sum to the target\n");
    }
   }

   fclose(ifp);
   return 0;
}

示例输入文件:

20
19 4 7 8
10 1 9 6
14 3 8 3
2000000000 1 2 3
16 4 4 8
16 8 4 4
16 8 5 3
16 3 5 8
16 3 8 5
16 5 3 8
16 5 8 3
16 8 3 5
6 1 2 3
24 7 8 9
3 1 1 1
27 9 9 9
1 1 1 1
9 9 9 9
22 6 7 9
12 3 4 5

最佳答案

您需要引用输入:

fscanf(ifp, "%d%d%d%d", &target, &n1, &n2, &n3);

此外,您的错误消息与您的问题中的错误消息不匹配,但以上是您的主要问题。

关于c - 输出不正确: determine if three digits are unique and add up to a given sum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537364/

相关文章:

c - Sublime Text 3 在 OS X 上使用传递的参数编译并运行(在终端中)C

c - 使用 typeof 将变量声明转换为类型?

c++ - 即使满足并输入退出条件,仍然打印错误消息

c - 替换c中二维数组中的值

c - fscanf() 函数错误

c - 使用双指针在 C 中插入链表

c - 在 iPhone 中启动 TTPhotoviewController 时收到 SIGABRT 错误

C:解析输入文件以检查格式

c++ - OpenMP导致heisenbug segfault

c++ - 在iOS上执行pthread_cancel时,堆中的内存会被释放吗?