PAT/字符串處理習題集(一)


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 }

 

B1031. 查驗身份證 (15)

Description:

一個合法的身份證號碼由17位地區、日期編號和順序編號加1位校驗碼組成。校驗碼的計算規則如下:

首先對前17位數字加權求和,權重分配為:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};然后將計算的和對11取模得到值Z;最后按照以下關系對應Z值與校驗碼M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2

現在給定一些身份證號碼,請你驗證校驗碼的有效性,並輸出有問題的號碼。

Input:

輸入第一行給出正整數N(<= 100)是輸入的身份證號碼的個數。隨后N行,每行給出1個18位身份證號碼。

Output:

按照輸入的順序每行輸出1個有問題的身份證號碼。這里並不檢驗前17位是否合理,只檢查前17位是否全為數字且最后1位校驗碼計算准確。如果所有號碼都正常,則輸出“All passed”。

Sample Input1:

4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X

Sample Output1:

12010X198901011234
110108196711301866
37070419881216001X

Sample Input2:

2
320124198808240056
110108196711301862

Sample Output2:

All passed

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 int w[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};  5 char change[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};  6 
 7 int main()  8 {  9     int n; 10     char str[20]; 11     bool flag = true; 12     scanf("%d", &n); 13     for(int i=0; i<n; ++i) { 14         scanf("%s", str); 15         int j, last = 0; 16         for(j=0; j<17; ++j) { 17             if(!(str[j]>='0' && str[j]<='9'))   break; 18             last += (str[j]-'0')*w[j]; 19  } 20         if(j < 17) { 21             flag = false; 22             printf("%s\n", str); 23         } else { 24             if(change[last%11] != str[17]) { 25                 flag = false; 26                 printf("%s\n", str); 27  } 28  } 29  } 30     if(flag == true)    printf("All passed\n"); 31 
32     return 0; 33 }

 

B1002. 寫出這個數 (20)

Description:

讀入一個自然數n,計算其各位數字之和,用漢語拼音寫出和的每一位數字。

Input:

每個測試輸入包含1個測試用例,即給出自然數n的值。這里保證n小於10100

Output:

在一行內輸出n的各位數字之和的每一位,拼音數字間有1 空格,但一行中最后一個拼音數字后沒有空格。

Sample Input:

1234567890987654321123456789

Sample Output:

yi san wu

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 int main()  5 {  6     char str[110];  7  gets(str);  8 
 9     int ans[10], num = 0, len = strlen(str); 10     int sum = 0; 11     for(int i=0; i<len; ++i) 12         sum += str[i]-'0'; 13     while(sum != 0) { 14         ans[num++] = sum%10; 15         sum /= 10; 16  } 17     char change[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; 18 
19     for(int i=num-1; i>=0; i--) { 20         printf("%s", change[ans[i]]); 21         if(i != 0)  printf(" "); 22         else printf("\n"); 23  } 24 
25     return 0; 26 }

 

B1009. 說反話 (20)

Description:

給定一句英語,要求你編寫程序,將句中所有單詞的順序顛倒輸出。

Input:

測試輸入包含一個測試用例,在一行內給出總長度不超過80的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用1個空格分開,輸入保證句子末尾沒有多余的空格。

Output:

每個測試用例的輸出占一行,輸出倒序后的句子。

Sample Input:

Hello World Here I Come

Sample Output:

Come I Here World Hello

 1 #include <cstdio>
 2 
 3 #define MaxSize 100
 4 char List[MaxSize][MaxSize];  5 
 6 int main()  7 {  8     //freopen("E:\\Temp\\input.txt", "r", stdin);
 9 
10     int num = 0; 11     while(scanf("%s", List[num]) != EOF) 12         num++; 13 
14     for(int i=num-1; i>=0; --i) { 15         printf("%s", List[i]); 16         if(i != 0)  printf(" "); 17         else printf("\n"); 18  } 19 
20     return 0; 21 }
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MaxSize 100
 5 char List[MaxSize], ans[MaxSize][MaxSize];  6 
 7 int main()  8 {  9     //freopen("E:\\Temp\\input.txt", "r", stdin);
10 
11  gets(List); 12 
13     int num = 0, counter = 0, len = strlen(List); 14     for(int i=0; i<len; ++i) { 15         if(List[i] != ' ') 16             ans[num][counter++] = List[i]; 17         else { 18             counter = 0; 19             ++num; 20  } 21  } 22 
23     for(int i=num; i>=0; --i) { 24         printf("%s", ans[i]); 25         if(i != 0)  printf(" "); 26         else printf("\n"); 27  } 28 
29     return 0; 30 }

 

B1014. 福爾摩斯的約會 (20)

Description:

大偵探福爾摩斯接到一張奇怪的字條:“我們約會吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”。大偵探很快就明白了,字條上奇怪的亂碼實際上就是約會的時間“星期四 14:04”,因為前面兩字符串中第1對相同的大寫英文字母(大小寫有區分)是第4個字母'D',代表星期四;第2對相同的字符是'E',那是第5個英文字母,代表一天里的第14個鍾頭(於是一天的0點到23點由數字0到9、以及大寫字母A到N表示);后面兩字符串第1對相同的英文字母's'出現在第4個位置(從0開始計數)上,代表第4分鍾。現給定兩對字符串,請幫助福爾摩斯解碼得到約會的時間。

Input:

輸入在4行中分別給出4個非空、不包含空格、且長度不超過60的字符串。

Output:

在一行中輸出約會的時間,格式為“DAY HH:MM”,其中“DAY”是某星期的3字符縮寫,即MON表示星期一,TUE表示星期二,WED表示星期三,THU表示星期四,FRI表示星期五,SAT表示星期六,SUN表示星期日。題目輸入保證每個測試存在唯一解。

Sample Input:

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

Sample Output:

THU 14:04

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 int main()  5 {  6     char week[7][5] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};  7     char str1[70], str2[70], str3[70], str4[70];  8  gets(str1), gets(str2), gets(str3), gets(str4);  9 
10     int len1 = strlen(str1), len2 = strlen(str2), len3 = strlen(str3), len4 = strlen(str4); 11     int i; 12     for(i=0; i<len1&&i<len2; ++i) { 13         if(str1[i]==str2[i] && str1[i]>='A' && str1[i]<='G') { 14             printf("%s ", week[str1[i]-'A']); 15             break; 16  } 17  } 18     for(++i; i<len1&&i<len2; ++i) { 19         if(str1[i] == str2[i]) { 20             if(str1[i]>='0' && str1[i]<='9') { 21                 printf("%02d:", str1[i]-'0'); 22                 break; 23             } else if(str1[i]>='A' && str1[i]<='N') { 24                 printf("%02d:", str1[i]-'A'+10); 25                 break; 26  } 27  } 28  } 29     for(i=0; i<len3&&i<len4; ++i) { 30         if(str3[i] == str4[i]) { 31             if((str3[i]>='A' && str3[i]<='Z') || (str3[i]>='a'&&str3[i]<='z')) { 32                 printf("%02d", i); 33                 break; 34  } 35  } 36  } 37 
38     return 0; 39 }

 

A1061. Dating (20)

Description:

Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm". It took him only a minute to figure out that those strange strings are actually referring to the coded time "Thursday 14:04" -- since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter 'D', representing the 4th day in a week; the second common character is the 5th capital letter 'E', representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is 's' at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output:

For each test case, print the decoded time in one line, in the format "DAY HH:MM", where "DAY" is a 3-character abbreviation for the days in a week -- that is, "MON" for Monday, "TUE" for Tuesday, "WED" for Wednesday, "THU" for Thursday, "FRI" for Friday, "SAT" for Saturday, and "SUN" for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE
2984akDfkkkkggEdsb
s&hgsfdk
d&Hyscvnm

Sample Output:

THU 14:04

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MaxSize 100
 5 char str1[MaxSize], str2[MaxSize], str3[MaxSize], str4[MaxSize];  6 char date[7][5] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};  7 
 8 int main()  9 { 10     //freopen("E:\\Temp\\input.txt", "r", stdin);
11 
12  gets(str1), gets(str2), gets(str3), gets(str4); 13 
14     int i = 0, len1 = strlen(str1), len2 = strlen(str2), len3 = strlen(str3), len4 = strlen(str4); 15     for(; i<len1&&i<len2; ++i) { 16         if(str1[i]==str2[i] && str1[i]>='A'&& str1[i]<='G') { 17             printf("%s ", date[str1[i]-'A']); 18             break; 19  } 20  } 21     for(++i; i<len1&&i<len2; ++i) { 22         if(str1[i]==str2[i]) { 23             if(str1[i]>='0' && str1[i]<='9') { 24                 printf("%02d:", str1[i]-'0'); 25                 break; 26             } else if(str1[i]>='A' && str1[i]<='N') { 27                 printf("%02d:", str1[i]-'A'+10); 28                 break; 29  } 30  } 31  } 32     for(i=0; i<len3&&i<len4; ++i) { 33         if(str3[i] == str4[i]) { 34             if((str3[i]>='a'&&str1[i]<='z') || (str3[i]>='A'&&str3[i]<='Z')) { 35                 printf("%02d", i); 36                 break; 37  } 38  } 39  } 40 
41     return 0; 42 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM