在线编译器 C

#include <stdio.h> int main() { int n,digit=0; scanf("%d",&n); int safe_n=n; while(digit<=9){ n=safe_n; int counter=0; while(n>0){ int current_digit=n%10; if(current_digit=digit){ counter++; } n=n/10; } if(counter>=2){ printf("YES"); return 0; } digit++; } printf("NO"); return 0; }
这段代码的目的是判断一个整数中是否存在某个数字(0-9)出现了至少两次。但代码中存在一个关键错误,导致它无法正确工作。

- 在第11行,`if(current_digit=digit)` 使用了赋值运算符 `=` 而不是比较运算符 `==`。这会导致 `current_digit` 被赋值为 `digit` 的值,并且条件永远为真(只要赋值后的值非零),所以 `counter` 会错误地增加。应该使用 `==` 来比较两个值是否相等。

- 另外,当输入的数字为0时,内层循环 `while(n>0)` 不会执行,因此数字0的出现次数不会被统计。如果输入的数字本身就是0,或者包含多个0,程序可能无法正确判断。