B1006. 換個格式輸出整數 (15)
Description:
讓我們用字母B來表示“百”、字母S表示“十”,用“12...n”來表示個位數字n(<10),換個格式來輸出任一個不超過3位的正整數。例如234應該被輸出為BBSSS1234,因為它有2個“百”、3個“十”、以及個位的4。
Input:
每個測試輸入包含1個測試用例,給出正整數n(<1000)。
Output:
每個測試用例的輸出占一行,用規定的格式輸出n。
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. 個位數統計 (15)
Description:
給定一個k位整數N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),請編寫程序統計每種不同的個位數字出現的次數。例如:給定N = 100311,則有2個0,3個1,和1個3。
Input:
每個輸入包含1個測試用例,即一個不超過1000位的正整數N。
Output:
對N中每一種不同的個位數字,以D:M的格式在一行中輸出該位數字D及其在N中出現的次數M。要求按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 }