PAT/字元串處理習題集(二)

来源:http://www.cnblogs.com/VincentValentine/archive/2016/11/18/6068220.html
-Advertisement-
Play Games

B1024/A1073/A1001/A1005/A1035/A1077/A1082 ...


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 }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • java中的八種基本數據類型: 整形: byte 、short、int、long 浮點型: double、float 字元型: char 布爾: boolean 字元串: String(引用數據類型) 聲明變數的方法: 方式一:數據類型 變數名 = 數據; 方法二:數據類型 變數名1,變數名2... ...
  • list() 用於在一次操作中給一組變數賦值。 註釋:list()只用於數字索引的數組,且假定數字索引從 0 開始。 說明 list() 用數組中的元素為一組變數賦值。 註意,與 array() 類似,list() 實際上是一種語言結構,不是函數。 如: <?php $my_array = arra ...
  • 轉自:https://www.douban.com/note/145065606/ 《省得每次都得去翻麻煩》 過濾器,變數的顯示形式的改變一、形式:小寫{{ name | lower }} 二、串聯:先轉義文本到HTML,再轉換每行到 <p> 標簽{{ my_text|escape|linebrea ...
  • 有三種方法可以使一個Action處理多個請求 這裡就說一下Dynamic Method nvocation ,動態方法調用,什麼是動態方法調用呢,就是一個Action裡面包含多個方法調用 一般情況下,我們只需要在Action中寫一個execute的方法,並返回String 就可以了,或者繼承Acti ...
  • 服務端 rest api ...
  • 我們做網站的時候經常要用到excel導入和導出的功能,我們通常的做法是用phpexcel工具包來完成,具體方法如下: html代碼: 寫ExcelController工具類:此類用來被實例化 下麵書寫控制器來上傳excel表格:此類方法不用把excel表格傳遞到伺服器,直接寫入數據: ...
  • 下載python: 從從https://www.python.org/downloads/下載python,根據操作系統的不同,選擇不同的版本下載。註意:linux系統大多預裝了python,可以直接使用。 ubuntu16.04中已安裝python2.7和python3.x版本,輸入python和 ...
  • socket通信的原理在這裡就不說了,它的用途還是比較廣泛的,我們可以使用socket來做一個API介面出來,也可以使用socket來實現兩個程式之間的通信,我們來研究一下在php裡面如何實現socket通信。 由於socket服務端的代碼要監聽埠,等待接收請求,所以php在做socket服務的時 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...