1082. Read Number in Chinese (25)

1.非常好的一道模拟题
2.所谓模拟题就是根据题意写程序,许多模拟题都会有一堆if else
3.能否简化就在于如何在短时间内对题目有一个深入的理解

1082. Read Number in Chinese (25)
时间限制 
400 ms
内存限制 
65536 kB
代码长度限制 
16000 B
判题程序 
Standard 
作者 
CHEN, Yue
Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".
Input Specification: 
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification: 
For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.
Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai

1.刚开始思路就是四个一组(举例:后四位,中间四位,最前面一位,10E9的时候)判断,但是在面临空格,深感压力,骗了9分,遂放弃,百度了资料
2.代码见下,很好的字符串题
3.这种题目完全不知道怎么练 骗一点分还是可以的
4.记住,if else 太多会导致你自己都不知道你在写什么,建议先想好方法再写程序,函数打包最好

#include <bits/stdc++.h>
using namespace std;
char *aa[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
char *ss[]={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
int main(){
    string str;
    cin>>str;
    if(str[0]=='-'){
        printf("Fu ");
        str=str.substr(1);
    }
    reverse(str.begin(),str.end());
    int flag=0;
    for(int i=str.size()-1;i>=1;i--){
        if(i==str.size()-1){ //输出第一个数字,由于空格,特殊处理
            cout<<aa[str[i]-'0'];
            cout<<" "<<ss[i];
        }
        else{
            if(str[i]!='0'){  //遇到不为零的
                if(flag==1){ //要是前面有0,输出一个 ling 同时修改flag值
                    flag=0;
                    cout<<" "<<aa[0];
                }
                cout<<" "<<aa[str[i]-'0'];  //输出非零数字
                cout<<" "<<ss[i];
            }
            else{//有0存在flag=1 特殊情况就是1,0800,0000 八百后面需要一个万
                flag=1;
                if(i==4)
                    cout<<" "<<ss[4];
            }
        }
    }
    if(flag&&str[0]!='0'){  //有零存在且最后一位不为0
        printf(" ling");
    }
    if(str.size()==1){ //如果长度为1,原样输出
        cout<<aa[str[0]-'0'];
    }
    else if(str.size()>1&&str[0]!='0'){  //长度为1且最后一位不为零,输出最后一位
        cout<<" "<<aa[str[0]-'0'];
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38677814/article/details/80158506