#include <stdio.h>
#include <string.h>
int main(void) {
int i, j, t;
scanf("%d", &t); // enter the number of test cases
getchar();
char input[11111];
for(i=0; i<t; i++){
scanf("%[^STOP]", input); // take input till STOP will come
printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
}
return 0;
}
最佳答案
将代码更改为
#include <stdio.h>
#include <string.h>
int main(void) {
int i, j, t;
char ch;
scanf("%d", &t); // enter the number of test cases
getchar();
char input[11111];
for(i=0; i<t; i++){
scanf("%[^STOP]", input); // take input till STOP will come
while((ch=getchar())!='\n'&& ch!= EOF);
printf("%s\n", input);
// this code print first input as many time as number of test cases in the code that will we provide through variable t;
}
return 0;
}
该问题是由于缓冲引起的。 scanf()
将 \n
发送到输入缓冲区,下一个 scanf()
读取它。这就是造成问题的原因。
如果您想了解更多信息,只需在 Stack Overflow 或 Google 中搜索 input buffer 即可。这个问题已经被讨论过很多次了。
关于c - 为什么无论测试用例的数量如何,都会重复打印第一个字符串以及如何更正它。请更正。使用c语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670393/