一个想法

P1307 [NOIP2011 普及组] 数字反转

划水的小伍 @ 2021-12-24 18:59:07

数字反转这道题我觉得好像可以这样做:

#include<cstdio>
#include<cstring>
const int MAXN=10000+11;
char s[MAXN];
int t;
int main()
{
    scanf("%s", &s);
    if(s[0]=='0')
    {
        printf("0");
        return 0;
    }
    int len=strlen(s);
    t=len;
    if(s[0]=='-')
        printf("-");
    for(int i=len-1;i>=0;i--)
    {
        if(s[i]=='0')
            t--;
        else
            break;
    }
    while(true)
    {
        if(s[t-1]=='-' || t<=0)
            break;
        printf("%c", s[t-1]);
        t--;
    }
    return 0;
}

by HarunluoON @ 2021-12-24 19:07:13

对啊


by zhanghanchu @ 2021-12-24 19:11:34

不是还有更简单的做法吗?

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,s=0;
    cin>>n;
    while(n)
    {
        s=s*10+n%10;
        n/=10;
    }
    cout<<s;
    return 0;
}

by OldVagrant @ 2021-12-24 19:14:35

其实可以直接倒序输出字符串


by OldVagrant @ 2021-12-24 19:14:46

加个特判负数和0就好


by keepoing @ 2021-12-31 17:30:39

题目不是要输整数吗,不按题目来是不行的


|