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.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (≤10100).
Output Specification:
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.
Sample Input:
12345
Sample Output:
one five
Experiential Summing-up
一道处理字符串的题。将字符串转化为 int 类型,每一位累加,最后将和 sum 转化为 string 类型,输出时再转化为 int 类型。
to_string:将整形转换成字符串。如 string num=to_string(sum)。
Accepted Code
1 #include<bits/stdc++.h> 2 using namespace std; 3 int sum; 4 string a, s; 5 string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", 6 "eight", "nine"}; 7 8 int main() 9 { 10 cin >> a; 11 for(int i = 0; i < a.length(); i ++) 12 sum += (a[i] - '0'); 13 s = to_string(sum); 14 cout << arr[s[0] - '0']; 15 for(int i = 1; i < s.length(); i ++) 16 cout << " " << arr[s[i] - '0']; 17 return 0; 18 }
标签:case,10,Right,PAT,string,int,sum,Spell,line From: https://www.cnblogs.com/marswithme/p/17149303.html