题目要求
解题思路
本题就是将给定的数字,把每一位的数值相加然后用英文输出。那首先想到就是字符串接收输入,然后通过—“0”
操作来求和,再通过to_string()
函数变为string类型,最后还是通过—“0”
操作得到数组下标。
实现代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<sstream>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
string str;
cin >> str;
int sum = 0;
for (size_t i = 0; i < str.size(); i++)
{
//这个地方,就是将字符串"0"变为数字0的方式
sum += (str[i] - '0');
}
string tmp = to_string(sum);
string arr[] = { "zero","one","two","three","four","five","six","seven","eight","nine" };
for (size_t i = 0; i < tmp.size(); i++)
{
if (i == 0) cout << arr[(tmp[i]-'0')];
else
cout << " " << arr[(tmp[i]-'0')];
}
system("pause");
return 0;
}
总结
该题主要操作就是通过—“0”
操作,将字符类型的数字变为整型数字。
Comments | NOTHING