PAT/字符串处理习题集(二)

2018-06-18 04:13:16来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

B1024. 科学计数法 (20)

Description:

科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+-][0-9]+,即数字的整数部分只有1位,小数部分至少有1位,该数字及其指数部分的正负号即使对正数也必定明确给出。

现以科学计数法的格式给出实数A,请编写程序按普通数字表示法输出A,并保证所有有效位都被保留。

Input:

每个输入包含1个测试用例,即一个以科学计数法表示的实数A。该数字的存储长度不超过9999字节,且其指数的绝对值不超过9999。

Output:

对每个测试用例,在一行中按普通数字表示法输出A,并保证所有有效位都被保留,包括末尾的0。

Sample Input1:

+1.23400E-03

Sample Output1:

0.00123400

Sample Input2:

-1.2E+10

Sample Output2:

-12000000000

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 int main()
 5 {
 6     char str[10010];
 7     gets(str);
 8 
 9     int len = strlen(str);
10     if(str[0] == '-')   printf("-");
11     int pos = 0;
12     while(str[pos] != 'E')    ++pos;
13     int exp = 0;
14     for(int i=pos+2; i<len; ++i)    exp = exp*10+(str[i]-'0');
15     if(exp == 0) {
16         for(int i=1; i<pos; ++i)
17             printf("%c", str[i]);
18     }
19     if(str[pos+1] == '-') {
20         printf("0.");
21         for(int i=0; i<exp-1; ++i)  printf("0");
22         printf("%c", str[1]);
23         for(int i=3; i<pos; ++i)    printf("%c", str[i]);
24     } else {
25         for(int i=1; i<pos; ++i) {
26             if(str[i] == '.')   continue;
27             printf("%c", str[i]);
28             if(i==exp+2 && pos-3!=exp)  printf(".");
29         }
30         for(int i=0; i<exp-(pos-3); ++i)    printf("0");
31     }
32 
33     return 0;
34 }

 

A1073. Scientific Notation (20)

Description:

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9]"."[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input:

Each input file contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.

Output:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros,

Sample Input1:

+1.23400E-03

Sample Output1:

0.00123400

Sample Input2:

-1.2E+10

Sample Output2:

-12000000000

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 int main()
 5 {
 6     char str[10010];
 7     gets(str);
 8 
 9     int len = strlen(str);
10     if(str[0] == '-') {
11         printf("-");
12     }
13     int pos = 0;
14     while(str[pos] != 'E') {
15         ++pos;
16     }
17     int exp = 0;
18     for(int i=pos+2; i<len; ++i) {
19         exp = exp*10+str[i]-'0';
20     }
21     if(exp == 0) {
22         for(int i=1; i<pos; ++i) {
23             printf("%c", str[i]);
24         }
25     }
26     if(str[pos+1] == '-') {
27         printf("0.");
28         for(int i=0; i<exp-1; ++i) {
29             printf("0");
30         }
31         printf("%c", str[1]);
32         for(int i=3; i<pos; ++i) {
33             printf("%c", str[i]);
34         }
35     } else {
36         for(int i=1; i<pos; ++i) {
37             if(str[i] == '.') {
38                 continue;
39             }
40             printf("%c", str[i]);
41             if(i==exp+2 && pos-3!=exp) {
42                 printf(".");
43             }
44         }
45         for(int i=0; i<exp-(pos-3); ++i) {
46             printf("0");
47         }
48     }
49 
50     return 0;
51 }

 

A1001. A+B Format (20)

Description:

Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input:

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

-999,991

 1 #include <cstdio>
 2 
 3 int main()
 4 {
 5     int a, b, num[10] = {0};
 6     scanf("%d%d", &a, &b);
 7 
 8     int len = 0, sum = a+b;
 9     if(sum < 0) {
10         printf("-");
11         sum = -sum;
12     }
13     do {
14         num[len++] = sum%10;
15         sum /= 10;
16     } while(sum != 0);
17     for(int k=len-1; k>=0; --k) {
18         printf("%d", num[k]);
19         if(k>0 && k%3==0) {
20             printf(",");
21         }
22     }
23 
24     return 0;
25 }

 

A1005. Spell It Right (20)

Description:

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input:

Each input file contains one test case. Each case occupies one line which contains an N (<= 10100).

Output:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 char s[111], num[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
 5 int digit[10];
 6 
 7 int main()
 8 {
 9     gets(s);
10 
11     int sum = 0, numLen = 0, len = strlen(s);
12     for(int i=0; i<len; ++i)    sum += (s[i]-'0');
13     do {
14         digit[numLen++] = sum%10;
15         sum /= 10;
16     } while(sum != 0);
17 
18     for(int i=numLen-1; i>=0; --i) {
19         printf("%s", num[digit[i]]);
20         if(i != 0)  printf(" ");
21     }
22 
23     return 0;
24 }

 

A1035. Password (20)

Description:

To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) from O (o in uppercase). One solution is to replace 1 (one) by @, 0 (zero) by %, l by L, and O by o. Now it is your job to write a program to check the accounts generated by the judge, and to help the juge modify the confusing passwords.

Input:

Each input file contains one test case. Each case contains a positive integer N (<= 1000), followed by N lines of accounts. Each account consists of a user name and a password, both are strings of no more than 10 characters with no space.

Output:

For each test case, first print the number M of accounts that have been modified, then print in the following M lines the modified accounts info, that is, the user names and the corresponding modified passwords. The accounts must be printed in the same order as they are read in. If no account is modified, print in one line "There are N accounts and no account is modified" where N is the total number of accounts. However, if N is one, you must print "There is 1 account and no account is modified" instead.

Sample Input1:

3
Team000002 Rlsp0dfa
Team000003 perfectpwd
Team000001 R1spOdfa

Sample Output1:

2
Team000002 RLsp%dfa
Team000001 R@spodfa

Sample Input2:

1
team110 abcdefg332

Sample Output2:

There is 1 account and no account is modified

Sample Input3:

2
team110 abcdefg222
team220 abcdefg333

Sample Output3:

There are 2 accounts and no account is modified

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 struct node {
 5     char name[20], password[20];
 6     bool ischange;
 7 } T[1005];
 8 
 9 void crypt(node &t, int &cnt) {
10     int len = strlen(t.password);
11     for(int i=0; i<len; ++i) {
12         if(t.password[i] == '1') {
13             t.password[i] = '@', t.ischange = true;
14         } else if(t.password[i] == '0') {
15             t.password[i] = '%', t.ischange = true;
16         } else if(t.password[i] == 'l') {
17             t.password[i] = 'L', t.ischange = true;
18         } else if(t.password[i] == 'O') {
19             t.password[i] = 'o', t.ischange = true;
20         }
21     }
22     if(t.ischange)  ++cnt;
23 }
24 
25 int main()
26 {
27     int n, cnt = 0;
28     scanf("%d", &n);
29     for(int i=0; i<n; ++i)
30         scanf("%s%s", T[i].name, T[i].password);
31 
32     for(int i=0; i<n; ++i) crypt(T[i], cnt);
33     if(cnt == 0) {
34         if(n == 1) printf("There is %d account and no account is modified\n", n);
35         else printf("There are %d accounts and no account is modified\n", n);
36     } else {
37         printf("%d\n", cnt);
38         for(int i=0; i<n; ++i) {
39             if(T[i].ischange)   printf("%s %s\n", T[i].name, T[i].password);
40         }
41     }
42 
43     return 0;
44 }

 

A1077. Kuchiguse (20)

Description:

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)
  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input:

Each input file contains one test case. For each case, the first line is an integer N (2<=N<=100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

Output:

For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write "nai".

Sample Input1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output1:

nyan~

Sample Input2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output2:

nai

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 char s[100][256];
 5 
 6 int main()
 7 {
 8 
 9     int n, minLen = 256, ans = 0;
10     scanf("%d", &n);
11     getchar();
12     for(int i=0; i<n; ++i) {
13         gets(s[i]);
14         int len = strlen(s[i]);
15         if(len < minLen)    minLen = len;
16         for(int j=0; j<len/2; ++j) {
17             char temp = s[i][j];
18             s[i][j] = s[i][len-j-1];
19             s[i][len-j-1] = temp;
20         }
21     }
22 
23     for(int i=0; i<minLen; ++i) {
24         char c = s[0][i];
25         bool same = true;
26         for(int j=1; j<n; j++) {
27             if(c != s[j][i]) {
28                 same = false;
29                 break;
30             }
31         }
32         if(same)    ++ans;
33         else break;
34     }
35 
36     if(ans) for(int i=ans-1; i>=0; --i) printf("%c", s[0][i]);
37     else printf("nai");
38 
39     return 0;
40 }

 

A1082. Read Number in Chinese (25)

Description:

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".

Input:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input1:

-123456789

Sample Output1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input1:

100800

Sample Output1:

yi Shi Wan ling ba Bai

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 char num[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"},
 5      wei[5][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};
 6 
 7 int main()
 8 {
 9     char str[15];
10     gets(str);
11 
12     int len = strlen(str);
13     int left = 0, right = len-1;
14     if(str[0] == '-') {
15         printf("Fu");
16         ++left;
17     }
18     while(left+4 <= right)  right -=4;
19     while(left < len) {
20         bool flag = false, isPrint = false;
21         while(left <= right) {
22             if(left>0 && str[left]=='0')    flag = true;
23             else {
24                 if(flag == true) {
25                     printf(" ling");
26                     flag = false;
27                 }
28                 if(left > 0)    printf(" ");
29                 printf("%s", num[str[left]-'0']);
30                 isPrint = true;
31                 if(left != right)   printf(" %s", wei[right-left-1]);
32             }
33             ++left;
34         }
35         if(isPrint==true && right!=len-1)   printf(" %s", wei[(len-1-right)/4+2]);
36         right += 4;
37     }
38 
39     return 0;
40 }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:值得学习的C/C++开源库和框架(转)

下一篇:数据结构概述