读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。
输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10100。
输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。
输入样例:
1234567890987654321123456789
输出样例:
yi san wu
代码:
#include <iostream>
#include <stack>
using namespace std;
int main(int argc, const char * argv[]) {
char numb[100];
for(int i = 0;i<100;i++)
numb[i]=0;
cin>>numb;
int sum = 0;
for(int i = 0;i<100;i++)
{
if(numb[i]!=0)
sum += numb[i] + 1 – ‘1’;
else
break;
}
bool first = true;
stack<int> mystack;
while(sum!=0)
{
int small = sum%10;
sum = sum/10;
mystack.push(small);
}
while (!mystack.empty()) {
int small = mystack.top();
mystack.pop();
if(first==false)
cout<<‘ ‘;
switch (small) {
case 0:
cout<<“ling”;
first = false;
break;
case 1:
cout<<“yi”;
first = false;
break;
case 2:
cout<<“er”;
first = false;
break;
case 3:
cout<<“san”;
first = false;
break;
case 4:
cout<<“si”;
first = false;
break;
case 5:
cout<<“wu”;
first = false;
break;
case 6:
cout<<“liu”;
first = false;
break;
case 7:
cout<<“qi”;
first = false;
break;
case 8:
cout<<“ba”;
first = false;
break;
case 9:
cout<<“jiu”;
first = false;
break;
default:
break;
}
}
return 0;
}