Topic request

Read in a positive integer n, calculate the sum of its digits, write down each digit of the sum in Chinese pinyin.

Input format:

Each test input contains 1 test case, which gives the value of the natural number N. I’m going to make sure that n is less than 10100.

Output format:

Output each digit of the sum of the digits of n in a line, with 1 space between the pinyin digits, but no space after the last pinyin digit in the line.

Example Input:

1234567890987654321123456789
Copy the code

Example output:

yi san wu
Copy the code

Algorithm 1

Analysis of the

1. Input is received with a string, and each digit in the string is summed to get sum

Sum %10, sum/10, sum%10, sum%10, sum%10, sum%10

3. Output backwards (see the number and the previous space as a whole)

C++

#include <iostream> #include <string> using namespace std; int main() { string str[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; string s; cin >> s; int sum = 0; For (int I = 0; for(int I = 0; i < s.length(); i++) { sum += s[i] - '0'; } int a[10]; int t = 0; While (sum) {a[t] = sum % 10; sum /= 10; t++; } cout << STR [a[t-1]]; for(int i = t-2; i >= 0; i--) { cout << " " << str[a[i]]; } return 0; }Copy the code

Algorithm 2

Analysis of the

1. Take input from string, sum each bit of string to get sum (int type)

2. Convert int to string

3. Output the Chinese pinyin corresponding to each digit from high to low

C++

#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string str[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; string s; cin >> s; int sum = 0; For (int I = 0; for(int I = 0; i < s.length(); i++) { sum += s[i] - '0'; } string s2; stringstream ss; ss << sum; ss >> s2; for(int i = 0; i < s2.length(); i++) { if(i ! = 0) cout << " "; cout << str[s2[i]- '0']; } return 0; }Copy the code

Pay attention to the point

If it is of character type, the ASCII code used as a normal number in the array or sum is subtracted from 0 **. 支那

Stringstream class

The header file:

<sstream>
Copy the code

(1). Convert int to string

int num; stringstream ss; string str; ss << num; ss >> str; // Convert int to stringCopy the code

(2). Convert string to int

string str = "17"; stringstream ss; int num; ss << str; ss >> num; // Convert string to int cout << ++num; //num = 18Copy the code