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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...