1005 Spell It Right
题目大意
算一下给出的一个数,里面各个位之和
算法思想
- 把数转换成字符串来处理,会更好一些,可以根据每一位来对应输出
- 但是判断时,只能一一对应来输出,目前没有更好的办法
- 没什么特殊点,写出来就全对
代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
string end;
int i, j;
int sum = 0;
cin >> s;
for (i = 0; i < s.size(); i++)//计算sum,可以利用ASCII值'0'
{
sum += s[i] - '0';
}
end = to_string(sum);//转为string类型好统计
for (i = 0; i < end.size(); i++)
{
if (end[i] == '0')//根据不同字符,来输出不同结果
cout << "zero";
else if (end[i] == '1')
cout << "one";
else if (end[i] == '2')
cout << "two";
else if (end[i] == '3')
cout << "three";
else if (end[i] == '4')
cout << "four";
else if (end[i] == '5')
cout << "five";
else if (end[i] == '6')
cout << "six";
else if (end[i] == '7')
cout << "seven";
else if (end[i] == '8')
cout << "eight";
else if (end[i] == '9')
cout << "nine";
if (i != end.size() - 1)//别忘了输出空格
cout << " ";
}
return 0;
}