题目链接:http://acm.swust.edu.cn/problem/0552/
Time limit(ms): 1000 Memory limit(kb): 65535
Description
数钱啦~~~输入一个数字,小于999999999999,我们需要把他们转换为拼音,我们规定例如1234000567的念为“1十2亿3千4百万零5百6十7”具体见输入输出
Input
一个数字N
Output
数字加拼音,见SAMPLE OUTPUT
Sample Input
999999999999
123456000
1234000567
|
Sample Output
9qian9bai9shi9yi9qian9bai9shi9wan9qian9bai9shi9
1yi2qian3bai4shi5wan6qian
1shi2yi3qian4baiwanling5bai6shi7
|
scpc——しιしιπ
解题思路:今晚上全是做的这一类题(大脑已经完全混乱了),就是把数字以字符串的形式输入,然后倒序,与这个字符数组
string p[] = { "","shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi", "bai", "qian" };匹配,重点就是注意多个零的处理,注意看代码~~
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 int ar[15], len; 6 long long n; 7 string s, p[] = { "", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi", "bai", "qian" }; 8 void change(){ 9 reverse(s.begin(), s.end()); 10 int i, flag = 0; 11 for (i = s.size() - 1; i >= 0;){ 12 if (s[i] - '0'){ 13 flag = 0; 14 cout << s[i] << p[i]; 15 i--; 16 } 17 else{ 18 //两个条件不能交换位置如果是6000,则最后一次i=-1,s[-1]无法访问(runtime error一次) 19 while (i >= 0 && !(s[i] - '0')){ 20 if (!(i % 4) && !flag){ 21 flag = 1; 22 cout << p[i]; 23 } 24 i--; 25 } 26 if (i >= 0)cout << "ling"; 27 } 28 } 29 cout << endl; 30 } 31 32 int main(){ 33 while (cin >> s) 34 change(); 35 return 0; 36 }