听取WA声一片

P1307 [NOIP2011 普及组] 数字反转

abababwosb @ 2024-06-27 16:37:42

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a, tag, ans;
    cin >> a;

    if (a == 0) {
        cout << a;
        return 0;
    } else if (a < 0) {
        tag = 1;
    } else {
        tag = 0;
    }
    while (a != 0) {
        ans = ans * 10 + a % 10;
        a /= 10;
    }

    if (tag == 1) {
        cout << -abs(ans);
    } else {
        cout << abs(ans);
    }
    return 0;
}

by KellyH @ 2024-06-27 17:19:01

哪题


by gaoyunxuan0917 @ 2024-06-27 17:19:39

给题目


by KellyH @ 2024-06-27 17:23:01

@abababwosb 用数组做


by Mouger_xhx @ 2024-06-27 17:25:41

ans声明=0不然数据会乱的


by abababwosb @ 2024-06-27 17:29:24

有道理


by KellyH @ 2024-06-27 17:30:36

@abababwosb 可以弄一个a[10000005]的数组 然后先while语句输入,别忘了做一个几位数的统计(int n=0)(n++) 最后再for循环倒序输出就好了


by KellyH @ 2024-06-27 17:31:51

@abababwosb 你这个没把ans初始化为0呀


by Jayden_121 @ 2024-06-27 18:31:41

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long x;
    cin>>x;
    if(x<0){
        //x==n;
        x = -x; 
        cout<<"-";
    }
    long long y=0; //①漏掉初始值0 
    while(x>0){//②x>=0会死循环,x的最终结果就是0 
        y=y*10+x%10;
        x=x/10;
    }
    cout<<y; 
    return 0;
}

|