PAT/string processing question set (1), pat question set
B1006. output integer in another format (15)
Description:
Let's use the letter B to represent "Hundred", the letter S to represent "Ten", and "12... n (<10) is a single digit. In another format, a positive integer of no more than three digits is output. For example, 234 should be output as BBSSS1234 because it has two "hundreds", three "Ten", and four digits.
Input:
Each test input contains one test case and a positive integer n (<1000 ).
Output:
The output of each test case occupies one line and outputs n in the specified format.
Sample Input1:
234
Sample Output1:
BBSSS1234
Sample Input2:
23
Sample Output2:
SS123
1 #include <cstdio> 2 3 int main() 4 { 5 int n; 6 scanf("%d", &n); 7 8 int num = 0, ans[5]; 9 while(n != 0) {10 ans[num++] = n%10;11 n /= 10;12 }13 14 for(int i=num-1; i>=0; --i) {15 if(i == 2) {16 for(int j=0; j<ans[i]; ++j)17 printf("B");18 } else if(i == 1) {19 for(int j=0; j<ans[i]; ++j)20 printf("S");21 } else {22 for(int j=1; j<=ans[i]; ++j)23 printf("%d", j);24 }25 }26 27 return 0;28 }
B1021. count (15)
Description:
Given a k-bit integer N = dk-1 * 10k-1 +... + d1 * 101 + d0 (0 <= di <= 9, I = 0 ,..., k-1, dk-1> 0), compile a program to count the number of occurrences of each individual digit. For example, given N = 100311, there are 2 0, 3 1, and 1 3.
Input:
Each input contains one test case, that is, a positive integer N with no more than 1000 bits.
Output:
For each digit in N, output the digit D and the number of times M appears in N in a row in the format of D: M. The output must be in ascending order of D.
Sample Input:
100311
Sample Output:
0: 2
1: 3
3:1
1 #include <cstdio> 2 #include <cstring> 3 4 #define MaxSize 1010 5 char List[MaxSize]; 6 7 int main() 8 { 9 //freopen("E:\\Temp\\input.txt", "r", stdin);10 11 gets(List);12 13 int len = strlen(List), ans[10] = {0};14 for(int i=0; i<len; ++i)15 ++ans[List[i]-'0'];16 17 for(int i=0; i<10; ++i) {18 if(ans[i] != 0)19 printf("%d:%d\n", i, ans[i]);20 }21 22 return 0;23 }