字符串处理:PAT1005 Spell It Right
作者:互联网
解题步骤:
1、数字的位数n很大,只能用字符串存储;
2、逐位把字符串中的字符转为数字后累加,将和值在转成一个新的字符串;
3、把新字符串的每个字符转换为数字后,用英文的形式输出。
代码实现:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
cin >> s;
int sum = 0;
for (int i = 0; i < s.size(); i++)
{
sum += s[i] - '0';
}
string ans[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
string ss = to_string(sum);
cout << ans[ss[0] - '0'];
for (int i = 1; i < ss.size(); i++)
{
cout << " " << ans[ss[i] - '0'];
}
return 0;
}
标签:Right,string,int,sum,Spell,字符串,include,PAT1005,数字 来源: https://blog.csdn.net/qq_48988844/article/details/120576221