PAT Level B 1002 Write this number

PAT Brush Questions Grade B 1002 (cpp)

I've been using PAT for a while, but I just didn't put it up before, and I created a lot of questions~ Sometimes when I write the latter questions, I will also use some of the previous things, but it is embarrassing to forget emmmmm!=. =, so I put it up for my own memory.

Problem Description

       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 inputContains 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

       For every person who outputs n in a line 数字之和的每一位, there is a space between the pinyin numbers, butThere is no space after the last pinyin number in a line

Input sample

1234567890987654321123456789 .

Sample output

yi san wu

problem analysis

       First, we first define a string array to store the pinyin from yi to jiu. Next, the upper limit of n given by this topic is really huge. The value range of int (positive number) is: 1 ~ 2 31 -1, long is also 32bit, and long long is about 1 ~ 2 63 -1. Although long double is 96bit, it is still very small compared to the upper limit. So let's 使用string或者char[]receive the input number. I prefer to use string, and use length in string to get the length of the string. We calculate the sum of all digits in a loop. Then you can convert the sum into a string to get each digit of the sum, or get each digit by calculating the remainder. The last is the output.

Code

The core code is as follows:

//num是string类型,用来存放输入数据
int length_of_num = num.length();   
for(int i=0;i< length_of_num;i++){
    
       //求和
	int t = int(num[i])-int('0');
	sum =sum + t;
}
while (sum > 0) {
    
      //获得输出结果
	int t = sum % 10;
	result[k] = number[t];
	k++;
	sum = sum / 10;
}

The complete code is implemented as follows:

The code is here, please move, little cute

Run implementation

Please don't complain about the cost of my time haha

Guess you like

Origin blog.csdn.net/ThunderF/article/details/90241020