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
", 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
", str);
23         } else {
24             if(change[last%11] != str[17]) {
25                 flag = false;
26                 printf("%s
", str);
27             }
28         }
29     }
30     if(flag == true)    printf("All passed
");
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("
");
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("
");
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("
");
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 }
原文地址:https://www.cnblogs.com/VincentValentine/p/6058400.html