题目链接: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" };匹配,重点就是注意多个零的处理,注意看代码~~
代码如下:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int ar[], len;
long long n;
string s, p[] = { "", "shi", "bai", "qian", "wan", "shi", "bai", "qian", "yi", "shi", "bai", "qian" };
void change(){
reverse(s.begin(), s.end());
int i, flag = ;
for (i = s.size() - ; i >= ;){
if (s[i] - ''){
flag = ;
cout << s[i] << p[i];
i--;
}
else{
//两个条件不能交换位置如果是6000,则最后一次i=-1,s[-1]无法访问(runtime error一次)
while (i >= && !(s[i] - '')){
if (!(i % ) && !flag){
flag = ;
cout << p[i];
}
i--;
}
if (i >= )cout << "ling";
}
}
cout << endl;
} int main(){
while (cin >> s)
change();
return ;
}