1005 Spell It Right
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Each input file contains one test case. Each case occupies one line which contains an N (≤10100).
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
12345
one five
初次总结:
这道题还是比较简单的,求出字符串中每个数字的总和,然后依次将每一位数用英语表示即可
代码:
- #include
- using namespace std;
-
- int main(){
- string s,res;
- cin >> s;
- string num[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
- int sum=0;
-
- for(char c:s) sum+=c-'0';
-
- res+=num[sum%10];
- sum/=10;
- while(sum){
- res=num[sum%10]+' '+res;
- sum/=10;
- }
-
- cout << res << endl;
-
- return 0;
- }
看看大佬的代码:
没有存储,直接输出,简单便捷,通俗易懂!
- #include
- using namespace std;
- int main() {
- string a;
- cin >> a;
- int sum = 0;
- for (int i = 0; i < a.length(); i++)
- sum += (a[i] - '0');
- string s = to_string(sum);
- string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- cout << arr[s[0] - '0'];
- for (int i = 1; i < s.length(); i++)
- cout << " " << arr[s[i] - '0'];
- return 0;
- }
好好学习,天天向上!
我要考研!