###**这题要用数组?** c++极简代码

P1307 [NOIP2011 普及组] 数字反转

tonylzq1 @ 2017-07-21 21:03:43

这题要用数组?

c++极简代码

#include <cstdio>
using namespace std;
int main()
{
    int n,x,y,ans=0,f=1;
    scanf("%d",&n);
    if (n<0){f=-1;n=-n;}
    x=n;
    while (x)//判0
    {
        y=x%10;//取出最低位
        ans=ans*10+y;//叠加答案
        x/=10;//删去最低位
    }
    printf("%d",ans*f);
    return 0;
}

by wcp1325273959 @ 2017-07-22 17:20:32

#include<iostream>
using namespace std;
int main()
{int k,m=0,n=0;cin>>k;
while(k)
{ m=k%10;k=k/10;n=n*10+m;}
cout<<n;
return 0;
}

by 暮光 @ 2017-07-24 12:58:26

这题需要额外变量?

#include <iostream>
using namespace std;
int main()
{
    int num;
    cin >> num;
    if (!num){
        cout << num;
        return 0;
    }
    if (num < 0){
        cout << '-';
        num = 0-num;
    }
    while (!(num%10))
        num /= 10;
    while (num){
        cout << num % 10;
        num /= 10;
    }
    return 0;
}

by ezoiHQM @ 2017-07-24 21:40:47

@ tonylzq1不需要using namespace std;


|