为什么第一个字符串会重复打印,而不考虑测试用例的数量以及如何更正.请更正.使用c语言


#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("%sn", 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("%sn", 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;
}

问题是由于缓冲造成的。CCD_ 1将CCD_ 2发送到输入缓冲器,并且下一个CCD_。这就是造成问题的原因。

如果你想了解更多关于这方面的信息,只需在Stack Overflow或谷歌中搜索输入缓冲区。这个问题已经讨论了很多次了。

最新更新