新手请教大佬

P1307 [NOIP2011 普及组] 数字反转

jimmyshi29 @ 2020-08-08 10:59:49

# include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n > 0 || n == 0)
    {
        int ans = 0;
        while (n != 0)
        {
            ans += n % 10;
            ans *= 10;
            n /= 10;
        }
        cout << ans << endl;
    }
    else
    {
        int ans = 0;
        n = -n;
                while (n != 0)
        {
            ans += n % 10;
            ans *= 10;
            n /= 10;
        }
        cout << -ans << endl;
    }
    return 0;
}

零分 (T_T


by jimmyshi29 @ 2020-08-08 11:03:22

额……我改对了


by 霜羽 @ 2020-08-08 11:03:44

ans先乘10再加上n的数位,不然最后ans会多个0


|