PAT Brush Questions Grade B 1006 Change the format to output integers

PAT Brush Questions Level B 1006 (cpp)

Title description

       Let us use the letter B to represent "hundred", the letter S to represent "ten", and use 12...n to represent the non-zero ones digit n (<10), and change the format to output any positive integer not exceeding 3 digits . For example, 234 should be output as BBSSS1234 because it has 2 "hundreds", 3 "tens", and 4 in the ones place.

Input format

       Each test input contains 1 test case, giving a positive integer n (<1000).

Output format

       The output of each test case occupies one line and outputs n in the prescribed format.

Input example 1

234

Sample output 1

BBSSS1234

Input example 2

23

Sample output 2

SS123

problem analysis

       The content of this question is simple and not difficult to implement. You only need to extract all the digits of the input number and output it. Wherein the input is a number of not more than three positive integer 而这个数有可能是个位数,也有可能是2位数或者是三位数. So we can use string to directly extract the digits of each person, use the length function to get the digits of the input number, and then perform operations; we can also use the remainder to operate, as long as the judgment conditions are well controlled.

Code

The core code is as follows:

while (num > 0) {
    
     //num是输入的数
		if (num >= 100) {
    
     //百位输出B
			for (int i = 0; i < num / 100; i++)  
				cout << "B";
			num = num % 100;
		}
		else if (num < 100 && num >= 10) {
    
      //十位输出S
			for (int i = 0; i < num / 10; i++)
				cout << "S";
			num = num % 10;
		}
		else {
    
       //各位输出数字
			for (int i = 1; i <= num; i++)
				cout << i;
			num = num % 1;
		}	
	}

The complete code is implemented as follows: the

       code is here~

Run implementation

Run implementation

Guess you like

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