PTA 1002题解
作者:互联网
思路分析:
由于数为大整数,所以我们选择用字符串来存储数据;
逐位累加得到要求的数;
正序逐位输出这个数;
对于中文输出,从0~9各列出用if语句判断输出;
代码如下:
/*
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字
*/
#include"bits/stdc++.h"
using namespace std;
int main() {
int m,sum=0;
string n;
cin >> n;
m = n.length();
for (int i = 0; i < m; i++)
sum += n[i] - '0';
int f = 0,tmp=sum,mask=1;
while (sum > 9) { //正序输出
sum /= 10;
mask *= 10;
}
while (mask!=0) {
int d=tmp/mask;
if (f)cout<<" ";
if (d % 10 == 0) cout << "ling";
if (d % 10 == 1) cout << "yi";
if (d % 10 == 2) cout << "er";
if (d % 10 == 3) cout << "san";
if (d % 10 == 4) cout << "si";
if (d % 10 == 5) cout << "wu";
if (d % 10 == 6) cout << "liu";
if (d % 10 == 7) cout << "qi";
if (d % 10 == 8) cout << "ba";
if (d % 10 == 9) cout << "jiu";
f = 1;
mask /= 10;
}
return 0;
}
标签:tmp,输出,int,题解,sum,mask,PTA,while,1002 来源: https://blog.csdn.net/A100_Jacker/article/details/123166676