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/