用C字符数组做的,特判0了为什么subtask#1还是TLE?(其他测试点全绿)

P1307 [NOIP2011 普及组] 数字反转

我也是用字符数组的,就最后一个点tle了,搞了半天,都快自闭了。最后给读取字符的循环加了个次数限制就过了,我也不知道为什么。 ```c #include <stdio.h> int main() { char N[13]; int index = 0, noz = 0,i=10; while (i--) {//加了个10次的次数限制 char ch = getchar(); /*每键入一个字符*/ if (ch == '\n') { /*若回车则跳出循环*/ break; } if (index < 11) { /*不超过10^9个数字*/ N[index++] = ch; } } N[index] = '\0'; /*在数组结尾添加null终止符使其成为完整的标准C数组*/ if (N[0]=='-') { /*负*/ printf("%c", '-'); for (int i = index - 1; i > 0; i--) { if (N[i] != '0') { /*第一个非零处改变状态*/ printf("%c", N[i]); noz = 1; } else if (noz == 1) { printf("%c", N[i]); } } } else if (N[0] == '0') { /*零*/ printf("%d", 0); } else { /*正*/ for (int i = index - 1; i >= 0; i--) { if (N[i]!='0') { printf("%c", N[i]); noz = 1; } else if(noz==1) { printf("%c", N[i]); } } } return 0; } ```
by Pu_241 @ 2023-10-05 19:04:53


|