zoukankan      html  css  js  c++  java
  • 老老实实 greeners'garden 数字反转

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {    
        string s;
        char p=0;                     //放符号。
        int cnt=0;
        cin>>s;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]>='0'&&s[i]<='9')
                cnt++;
            else
            {
                p=s[i];
                break;
            }
        }
        int x=cnt;  //x为符号前面的数(有的话)
        cnt--;
        while(s[cnt]=='0'&&cnt>0)     //去掉前面数字最后的0,使翻转后不出现前导零。
            cnt--;
        for(int i=cnt;i>=0;i--)
            cout<<s[i];
        if(p==0)                       //分别处理无符号、%、其他符号。
            return 0; 
        else
        {
            if(p=='%')
            {
                cout<<p;
                return 0;    
            }
            else
            cout<<p;
        }
        int m=s.size()-1;
        while(s[x+1]=='0'&&x<m-1)       //使x到m之间两端没有0,使得后面的数翻转后不出现前导零。
            x++;                        //  注意此处的   下标和数字次序关系。
        while(s[m]=='0'&&m>x+1)
            m--;
        for(int i=m;i>x;i--)
            cout<<s[i];
        return 0;            
    }
  • 相关阅读:
    Gym 101194L / UVALive 7908
    POJ 2259
    POJ 2559
    Gym 101194E / UVALive 7901
    Gym 101194D / UVALive 7900
    一种整数集上二分的正确写法
    日常训练记录
    Gym 101194C / UVALive 7899
    Gym 101194A / UVALive 7897
    HDU 5542
  • 原文地址:https://www.cnblogs.com/poo7/p/11802803.html
Copyright © 2011-2022 走看看