PAT Grade B Brush Record-1002 Write this number (20 points)

Read in a positive integer n, calculate the sum of its digits, and write 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. Here it is guaranteed that n is less than 10 100 .

Output format:

Each digit of the sum of the digits of n is output in one line, there is 1 space between the pinyin digits, but there is no space after the last pinyin digit in a line.

Sample input:

1234567890987654321123456789

Sample output:

yi san wu

Ideas

The meaning of the title is to add up each digit of the input string of numbers, and then output the result in Chinese Pinyin. For example, the sample given by the title should be 1 + 2 + 3 +… + 7 + 8 + 9 = 135 , So "yi san wu" is output.

Then just use string processing (or use C's character array), first read the whole string with the string, and then convert each bit of the string into an integer (-'0 'will do) Add them together, and finally convert the obtained results into strings, and then take the corresponding Chinese Pinyin for each bit (here I use a string array to make a one-to-one correspondence, the subscript is the corresponding integer, and The string taken by the subscript is the Chinese pinyin corresponding to that integer), see the code for details ~

Code

#include<string>
#include<iostream>
using namespace std;
char number[10][11]={"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
int main(){
    string tmp;
    cin>>tmp;
    long long sum = 0;
    for(int i=0;i<tmp.length();i++) sum += tmp[i]-'0';
    string result = to_string(sum);
    for(int i=0;i<result.length();i++){
        if(i==0) cout<<number[result[i]-'0'];
        else cout<<" "<<number[result[i]-'0'];
    }
    return 0;
}
Published 54 original articles · won 27 · views 4978

Guess you like

Origin blog.csdn.net/weixin_42257812/article/details/105521156