# C + +: PAT 1002 write this number, read it into a positive integer n, calculate the sum of its digits, and write each digit of the sum in Chinese pinyin.

Posted by Nilanka on Mon, 03 Jan 2022 03:15:50 +0100

# 1002 write the number C++

## subject

Read 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 one test case, which gives the value of the natural number n. Here we guarantee that n is less than 1 0 100 10^{100} 10100.

### Output format:

For each digit of the sum of the numbers of n output in a line, there is 1 space between the Pinyin digits, but there is no space after the last Pinyin digit in a line.

### Input example:

```1234567890987654321123456789
No blank lines at the end
```

### Output example:

```yi san wu
No blank lines at the end
```

## Code archiving

### 1.while method

```#include<iostream>
int main()
{
using namespace std;
const int ArSize = 100;
const int ArSize2 = 3;
char mathin1[ArSize];
int mathin2[ArSize2];
int sum1 = 0;
int i = 0;
int j = 0;

//Storage summation
while (mathin1[i] != '\n')//Read the numbers and sum them
{
switch (mathin1[i])//Conversion, converting characters to numbers
{
case '0':sum1 += 0; break;
case '1':sum1 += 1; break;
case '2':sum1 += 2; break;
case '3':sum1 += 3; break;
case '4':sum1 += 4; break;
case '5':sum1 += 5; break;
case '6':sum1 += 6; break;
case '7':sum1 += 7; break;
case '8':sum1 += 8; break;
default:sum1 += 9;
break;
}
i++;
};
//Decomposition sum
while (sum1 > 0)//Judgment condition: sum1 (sum of bits) is greater than zero. Decomposition steps
{
mathin2[j] = sum1 % 10;//Take the remainder of sum1, read the mantissa, save it to the array, and separate the numbers
sum1 /= 10;//Divide suml by 10 and move the remaining bit forward one bit next time
j++;
};
j--;
//Output Pinyin numbers
while(j>=0)
{
switch (mathin2[j])
{
case 0:cout << "ling"; break;
case 1:cout << "yi"; break;
case 2:cout << "er"; break;
case 3:cout << "san"; break;
case 4:cout << "si"; break;
case 5:cout << "wu"; break;
case 6:cout << "liu"; break;
case 7:cout << "qi"; break;
case 8:cout << "ba"; break;
case 9:cout << "jiu"; break;
};
if (j != 0)
cout << " ";
j--;
};
return 0;
}
```

result

Submission timestatefractionsubjectcompilertime consuminguser
2021/07/31 17:06:34The answer is correct20Programming problemC++ (clang++)7 ms
Test pointresultfractiontime consumingMemory
0The answer is correct126 ms440 KB
1The answer is correct16 ms320 KB
2The answer is correct15 ms320 KB
3The answer is correct27 ms456 KB
4The answer is correct26 ms376 KB
5The answer is correct26 ms320 KB

Steps:

According to the title, we need to input a positive integer n not greater than 10 ^ 100 into the program to sum its bits. It is required to use pinyin numbers to represent the digits of sum. There is a space between Pinyin numbers and no space at the end.

First, you need a container to store this potentially very large number. The upper limit of this number is 10 ^ 100.

Secondly, sum the bits of this number, which requires that each bit of the number be disassembled and stored to facilitate summation.

Thirdly, it is necessary to convert the bits of and into pinyin digital output, which requires the establishment of the relationship between the bits and Pinyin numbers.

Finally, a space is output after each Pinyin digit, and the last Pinyin digit is not output.

Using an array, you can store a very large number, and the characteristic of separate storage inside the array can also divide the digits on the number.

So, what format should array declaration bits be?

Here, I declare this array as a char type, regard this string of numbers as a string, and use the character array to read only one character to split this string of numbers.

Idea: Although the numbers of characters and arithmetic numbers are internally different on the computer, in our opinion, the form is the same. We can think that we can establish the relationship between the characters and numbers of the same shape and arithmetic numbers to realize the conversion. Finally, we can use this relationship to convert and sum.

code implementation

```//Storage summation
while (mathin1[i] != '\n')//Read the numbers and sum them
{
switch (mathin1[i])//Conversion, converting characters to numbers
{
case '0':sum1 += 0; break;
case '1':sum1 += 1; break;
case '2':sum1 += 2; break;
case '3':sum1 += 3; break;
case '4':sum1 += 4; break;
case '5':sum1 += 5; break;
case '6':sum1 += 6; break;
case '7':sum1 += 7; break;
case '8':sum1 += 8; break;
default:sum1 += 9;
break;
}
i++;
};
```

We can easily calculate the maximum value of the sum of the digits of this string, 99 * 9 = 891, which is a three digit number. We can take the remainder and divide the sum from right to left.

code implementation

```	while (sum1 > 0)//Judgment condition: sum1 (sum of bits) is greater than zero. Decomposition steps
{
mathin2[j] = sum1 % 10;//Take the remainder of sum1, read the mantissa, save it to the array, and separate the numbers
sum1 /= 10;//Divide suml by 10 and move the remaining bit forward one bit next time
j++;
};
j--;
```

The segmented and stored in the array, use switch to establish the connection between numbers and Pinyin numbers, so as to output them.

Since the position of the last bit in the array is 0, you can add a judgment statement that determines whether the number of bits in the array is equal to 0 to control the output space.

code implementation

```    //Output Pinyin numbers
while(j>=0)
{
switch (mathin2[j])
{
case 0:cout << "ling"; break;
case 1:cout << "yi"; break;
case 2:cout << "er"; break;
case 3:cout << "san"; break;
case 4:cout << "si"; break;
case 5:cout << "wu"; break;
case 6:cout << "liu"; break;
case 7:cout << "qi"; break;
case 8:cout << "ba"; break;
case 9:cout << "jiu"; break;
};
if (j != 0)
cout << " ";
j--;//Change cycle conditions
};
```

### 2. Use pointer

```#include<iostream>
int main();
const char* pin[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
int main()
{
using namespace std;
char* figure = new char[100];
int sum = 0;
int* sum1 = new int[3];
int i = 0, j = 0;

cin.get(figure[0]);
while (figure[i] != '\n')
{
sum = sum+ int(figure[i] - '0');
i++;
cin.get(figure[i]);
}
while (sum>0)//Split sum
{
sum1[j] = sum % 10;
sum /= 10;
j++;
}
j--;
while (j>=0)
{
cout << pin[sum1[j]];
if (j)
cout << " ";
j--;
}
return 0;
}
```
Submission timestatefractionsubjectcompilertime consuminguser
2021/08/01 12:54:29The answer is correct20Programming problemC++ (g++)5 ms
Test pointresultfractiontime consumingMemory
0The answer is correct125 ms432 KB
1The answer is correct15 ms324 KB
2The answer is correct15 ms316 KB
3The answer is correct24 ms316 KB
4The answer is correct24 ms452 KB
5The answer is correct25 ms320 KB

### 3. Delete pointer

```#include<iostream>
int main();
const char* pin[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
int main()
{
using namespace std;
char figure[1000];
int sum = 0;
int sum1[3];
int i = 0, j = 0;

cin >> figure;
while (figure[i] != '\0')
{
sum = sum + int(figure[i] - '0');
i++;
}
while (sum > 0)
{
sum1[j] = sum % 10;
sum /= 10;
j++;
}
j--;
while (j >= 0)
{
cout << pin[sum1[j]];
if (j)
cout << " ";
j--;
}
}
```

result:

Submission timestatefractionsubjectcompilertime consuminguser
2021/08/01 12:47:44The answer is correct20Programming problemC++ (g++)5 ms
Test pointresultfractiontime consumingMemory
0The answer is correct124 ms316 KB
1The answer is correct14 ms300 KB
2The answer is correct14 ms316 KB
3The answer is correct24 ms312 KB
4The answer is correct25 ms320 KB
5The answer is correct25 ms304 KB

Reference blog:

Topics: C++