70分求助

P1307 [NOIP2011 普及组] 数字反转

TheWindbell @ 2023-09-01 20:03:41

样例能过,但是70分。排查了好几遍了,求帮助。

#include <bits/stdc++.h>
#define maxn 20
char a[maxn];
int main()
{
    scanf("%s", &a);
    if(a[0]=='-')  printf("-");
    for(int i=strlen(a)-1; i>=(a[0]=='-'); i--) {
        if(a[i]!='0')  printf("%c", a[i]);
    }
    return 0;
} 

by willJ @ 2023-09-01 20:08:42

if(a[i]!='0')  printf("%c", a[i]);

它因该只是要你去前导零,你这是把0全去了吧?


by Rieman_sum @ 2023-09-01 20:09:02

-103

试试


by willJ @ 2023-09-01 20:13:57

@TheWindbell


by TheWindbell @ 2023-09-01 21:48:05

@willJ 谢谢!

最终代码:

#include <bits/stdc++.h>
#define maxn 20
char a[maxn];
int main()
{
    scanf("%s", &a);
    if(!strcmp(a, "0")) {
        printf("0");  return 0;
    }
    if(a[0]=='-')  printf("-");
    int mul = 1;
    for(int i=strlen(a)-1; i>=(a[0]=='-'); i--) {
        mul *= (a[i]=='0');
        if(mul==0)  printf("%c", a[i]);
    }
    return 0;
} 

by TheWindbell @ 2023-09-01 21:48:44

@Guo1 多谢提醒!


|