我很狡猾,用字符串做的。啊哈!

P1307 [NOIP2011 普及组] 数字反转

captainprice @ 2016-10-25 20:52:53

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char a[100];
    int len,i,t=0;
    gets(a);
    len=strlen(a);
    for(i=len-1;i>=0;i--)
    {
        if(a[i]==48)
            t++;
        else if(a[i]!=48)
            break;
    }
    if(a[0]=='-')
    {
        printf("-");
        for(i=len-t-1;i>=1;i--)
            printf("%c",a[i]);
    }
    else
        for(i=len-t-1;i>=0;i--)
            printf("%c",a[i]); 
    system("pause");
    return 0;
}

by acaca @ 2016-10-25 21:03:15

这题不就是用字符串嘛


by Lolierl @ 2016-10-25 21:13:49

这不如数位分离快


by DWVoid @ 2016-10-26 17:55:45

#include <iostream>
#include <memory.h>
#include <string.h>
using namespace std;
int main()
{
    char raw[32], rev[32];
    cin >> raw;
    bool neg = (raw[0] == '-'), flag = false;
    char* it = rev;
    if (neg)
    {
        for (int i = strlen(raw);  i > 1; --i)
            if (raw[i - 1] != '0' || flag)
            {
                *it = raw[i - 1];
                ++it;
                flag = true;
            }
        *it = 0;
        cout << "-" << rev <<endl;
    }
    else
    {
        for (int i = strlen(raw);  i > 0; --i)
            if (raw[i - 1] != '0' || flag)
            {
                *it = raw[i - 1];
                ++it;
                flag = true;
            }
        *it = 0;
        cout << rev <<endl;
    }
    return 0;
}

你那个过多的使用输入输出会拖慢速度


by nealchen @ 2016-11-03 22:03:16

#include <algorithm>
#include <stdio.h>
#include <string.h>
int n;
char s[233];
int main() {
    scanf("%d", &n);
    sprintf(s, "%d", n);
    std::reverse(s + (n < 0), s + strlen(s));
    sscanf(s, "%d", &n);
    printf("%d", n);
    return 0;
}
库函数大法好!

by 腾昊一世 @ 2016-11-09 19:37:21

#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(){
    char str1[100];
    int i,n,m;
    gets(str1+1);
    n=strlen(str1+1);
        m=n;
        while(str1[m]=='0'){
            m--;
        }
    if(str1[1]=='-'){
        printf("-");
        for(i=m;i>1;i--){
            printf("%c",str1[i]);
        }
    }
    else{
        for(i=m;i>=1;i--){
            printf("%c",str1[i]);
        }
    }
}
我也是

by NuclearBase_ACE @ 2016-11-12 18:31:26

#include<iostream>
#include<string> 
using namespace std;
int main()
{
    bool s = 1,c = 0;
    string a;
    cin>>a;
    if(a[0] == '-')
    {
        cout<<'-';
        c = 1;
    }
    for(int i = a.length()-1;i >= 0;i--)
    {
        if(a[i] == '0' && s)
        {} 
        else 
        {
            s = 0;
            if( c && i == 0)
            {}
            else 
                cout<<a[i];    
        } 
    }
    return 0;
}

|