1. 程式人生 > >ACM_一道耗時間的水題

ACM_一道耗時間的水題

sin rup positive == getchar 截取字符串 IT name sent

一道耗時間的水題

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

Do you know how to read the phone numbers in English? Now let me tell you.
For example, In China, the phone numbers are 11 digits, like: 15012233444. Someone divides the numbers into 3-4-4 format, i.e. 150 1223 3444. While someone divides the numbers into 3-3-5 format, i.e. 150 122 33444. Different formats lead to different ways to read these numbers:
150 1223 3444 reads one five zero one double two three three triple four.
150 122 33444 reads one five zero one double two double three triple four.
Here comes the problem:
Given a list of phone numbers and the dividing formats, output the right ways to read these numbers.
Rules:
Single numbers just read them separately.
2 successive numbers use double.
3 successive numbers use triple.
4 successive numbers use quadruple.
5 successive numbers use quintuple.
6 successive numbers use sextuple.
7 successive numbers use septuple.
8 successive numbers use octuple.
9 successive numbers use nonuple.
10 successive numbers use decuple.
More than 10 successive numbers read them all separately.

Input:

The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).
T test cases follow. Each line contains a phone number N(1 ≤ length of N ≤ 100) and the dividing format F, one or more positive integers separated by dashes (-), without zeros and whose sum always equals the number of digits in the phone number.

Output:

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the reading sentence in English whose words are separated by a space.

Sample Input:

3
15012233444 3-4-4
15012233444 3-3-5
12223 2-3

Sample Output:

Case #1: one five zero one double two three three triple four
Case #2: one five zero one double two double three triple four
Case #3: one two double two three
解題思路:這道題的題意比較簡單,處理起來比較麻煩,但掌握了這個知識點(C++的獲取子串的函數substr()),就簡單多了:
basic_string substr( size_type index, size_type num = npos );substr()返回本字符串的一個子串,從index開始,長num個字符。如果沒有指定,將是默認值 string::npos。這樣,substr()函數將簡單的返回從index開始的剩余的字符串。對應每個子串中某個字符出現的次數n如果為1時,則只輸出該字符對應的英語單詞;如果大於10,則輸出n個該字符對應的英語單詞;否則(n>1&&n<=10)輸出n對應倍數單詞和該字符對應的英語單詞。
AC代碼:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 105;//因為字符串s2中可能會出現100個‘1‘,所以這裏取105
 4 const string str1[]={"decuple","","double","triple","quadruple","quintuple","sextuple","septuple","octuple","nonuple"};
 5 const string str2[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
 6 int t,n,k,x,s[maxn],e[maxn];string s1,s2,tp[maxn];//s數組記錄截取字符串s1的起始位置,e數組記錄截取字符的個數
 7 int main(){
 8     cin>>t;getchar();//吃掉回車符對字符串的影響
 9     for(int i=1;i<=t;++i){
10         cin>>s1>>s2;s[0]=k=x=0;//s[0]起始位置為0,k記錄分成子串的個數
11         cout<<"Case #"<<i<<":";
12         for(unsigned int j=0;j<=s2.length();++j){
13             if(s2[j]==-||s2[j]==\0){e[k++]=x;x=0;}
14             else x=x*10+s2[j]-0;
15         }
16         for(int j=0;j<k;++j)s[j+1]=s[j]+e[j];//累加起始位置
17         for(int j=0;j<k;++j)tp[j]=s1.substr(s[j],e[j]);//獲得對應子串
18         for(int j=0;j<k;++j){n=1;//n記錄相同元素的個數,初始值為1
19             for(unsigned int h=0;h<tp[j].length();++h){
20                 if(tp[j][h]!=tp[j][h+1]){
21                     if(1<n&&n<=10)cout<<" "+str1[n%10];//取余操作
22                     if(n<=10)cout<<" "+str2[tp[j][h]-0];
23                     else{//n大於10,則輸出n個對應的字符串
24                         for(int m=1;m<=n;++m)
25                             cout<<" "+str2[tp[j][h]-0];
26                     }n=1;//n重置為1
27                 }
28                 else n++;//相同個數加1
29             }
30         }
31         cout<<endl;
32     }
33     return 0;
34 }

ACM_一道耗時間的水題