萌新想问问看这个为什么我全wa掉了,答案没问题啊

P1307 [NOIP2011 普及组] 数字反转

hellw @ 2019-07-22 18:35:55

include <iostream>

using namespace std;

int main(){ int flag=1,flag2=0; char what[10]={0,0,0,0,0,0,0,0,0,0}; cin>>what; if(what[0]=='-') { cout<<"-"; flag2=1; } for(int i=10;i>=0;i--){ if((flag&&what[i]=='0'&&i!=0)||what[i]=='-'||what[i]==0) continue; if(what[i]!='0'||!flag){ cout<<what[i]; flag=0; } if(what[0]=='0') cout<<"0"; } if(flag2&&flag) cout<<"0"; cout<<"\n"; return 0; }


by Irppy3_40 @ 2019-07-22 18:38:15

@hellw

希望更丰富的展现?使用Markdown


by hellw @ 2019-07-22 18:41:51

呃,第一次发帖没注意代码要重新选择,直接就粘上来了。。

#include <iostream>
using namespace std;

int main(){
    int flag=1,flag2=0;
    char what[10]={0,0,0,0,0,0,0,0,0,0};
    cin>>what;
    if(what[0]=='-') {
        cout<<"-";
        flag2=1;
    } 
    for(int i=10;i>=0;i--){
        if((flag&&what[i]=='0'&&i!=0)||what[i]=='-'||what[i]==0) continue;
        if(what[i]!='0'||!flag){
            cout<<what[i];
            flag=0;
        }
        if(what[0]=='0') cout<<"0";
    }
    if(flag2&&flag) cout<<"0"; 
    cout<<"\n";
    return 0;
}

by Irppy3_40 @ 2019-07-22 18:46:51

@hellw 其实这道题不用字符串还方便点,直接用int就行了


by hellw @ 2019-07-23 15:40:22

@wxh666haha emmmmmm好的,我可以换个方法,但是为什么我这个写出来自己测试答案没什么问题就是全wa??我想知道为什么


|