数字反转中出现的问题

img

img


其中存在runtime的错误,请大家解答一下是怎么回事?另外看一下我这个代码能不能符合题目的要求?有没有不用字符数组,只用for能实现数字反转

题目中已经明确N的取值范围,是在int类型范围内的,所以用int类型即可。循环求余,输出余数就可以了

#include <iostream>
using namespace std;
int main()
{
    int N,z=0;
    cin>>N;
    if(N < 0)
    {
        cout<<"-";
        N = N*-1;
    }
    while(N > 0)
    {
        if(z== 0 && N%10 != 0)
            z = 1;
        if(z==1)
            cout<<N%10;
        N = N/10;
    }
    return 0;
}