80分 菜鸡求救!!!

P1307 [NOIP2011 普及组] 数字反转

_Cupid @ 2019-12-19 21:14:49

#include<stdio.h>
int main(){
    int n,i;
    scanf("%d",&n);
    if(n < 0) {printf("-");n=-n;} 
    if(n == 0) {
        printf("0");
        return 0;
    }
    if(n%10 == 0) n /= 10;
    while(n){
        printf("%d",n%10);
        n /= 10;
    }
    return 0;

}

by Provicy @ 2019-12-19 21:18:44

你这后面大于一个0的时候不就挂了吗,那个if改成while


by loverintime @ 2019-12-19 22:02:03

楼上说得对


by Tino2023 @ 2020-02-28 13:02:26

@_Cupid 我10岁,学c++半年,AC代码

#include<iostream>
#include<string.h>
using namespace std;
int main(){
    char a[100];
    int flag=0;
    cin >> a;
    int len=strlen(a);
    if(a[0]!='-'){
        for(int i=len-1;i>=0;i--){
            if(a[i]=='0'&&flag==0){
                continue;
            }else{
                flag=1;
            }
            cout << a[i];
        }
    }else if(a[0]=='-'){
        cout << '-';
        for(int i=len-1;i>=1;i--){
            if(a[i]=='0'&&flag==0){
                continue;
            }else{
                flag=1;
            }
            cout << a[i];
        }
    }
    return 0;
}

by _Cupid @ 2020-03-08 16:06:40

@Mark的程序 小弟弟真棒,我80岁,从小学到大c++,不及你半分呢


by _Cupid @ 2020-03-08 16:09:01

@DXL 感想大佬


by _Cupid @ 2020-03-08 16:09:11

@xby070301 感谢大佬


|