题意
- 给定一个超大数,输出其各位数字总和的各位数字(英文单词)。
注意
- 使用字符串存储数。
单词
- consecutive 连续的
代码
#include <iostream>
#include <string>
using namespace std;
const int MAX = 101;
char big[MAX];
const string digit[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int main()
{
int p = 0;
int sum = 0;
while (cin >> big[p]) //CTRL+Z 结束输入
{
sum += big[p] - '0';
p++;
}
string s;
s = to_string(sum);
for (int i = 0; i < s.length(); i++)
{
cout << digit[s[i] - '0'];
if (i != s.length() - 1)
cout << " ";
}
return 0;
}
2
#include <iostream>
#include <string>
using namespace std;
string big;
const string digit[10] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int main()
{
int sum = 0;
cin >> big;
for (int i = 0; i < big.size(); i++) //C++ string size()和length()一样
sum += big[i] - '0';
string s = to_string(sum);
for (int i = 0; i < s.length(); i++)
{
cout << digit[s[i] - '0'];
if (i != s.length() - 1)
cout << " ";
}
return 0;
}