1. 程式人生 > >PAT L1 007 念數字

PAT L1 007 念數字

題目描述:

輸入一個整數,輸出每個數字對應的拼音。當整數為負數時,先輸出“fu”字。十個數字對應的拼音如下:

0: ling
1: yi
2: er
3: san
4: si
5: wu
6: liu
7: qi
8: ba
9: jiu
輸入格式:

輸入在一行中給出一個整數,如: 1234 。

提示:整數包括負數、零和正數。

輸出格式:

在一行中輸出這個整數對應的拼音,每個數字的拼音之間用空格分開,行末沒有最後的空格。如 yi er san si。

輸入樣例:
-600
輸出樣例:
fu liu ling ling

剛從006的餘波裡衝出來,看到這個題感覺舒服多了,然後看到自己當時的程式碼,覺得自己真的是傻逼….

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

using namespace std;
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  char
a[1000]; cin>>a; for(int i=0;a[i]!='\0';i++){ if(i==0){ if(a[i]=='-') cout<<"fu"; else if(a[i]=='0') cout<<"ling"; else if(a[i]=='1') cout<<"yi"; else if(a[i]=='2') cout<<"er"; else if(a[i]=='3') cout
<<"san"; else if(a[i]=='4') cout<<"si"; else if(a[i]=='5') cout<<"wu"; else if(a[i]=='6') cout<<"liu"; else if(a[i]=='7') cout<<"qi"; else if(a[i]=='8') cout<<"ba"; else cout<<"jiu"; } else{ if(a[i]=='0') cout<<" ling"; else if(a[i]=='1') cout<<" yi"; else if(a[i]=='2') cout<<" er"; else if(a[i]=='3') cout<<" san"; else if(a[i]=='4') cout<<" si"; else if(a[i]=='5') cout<<" wu"; else if(a[i]=='6') cout<<" liu"; else if(a[i]=='7') cout<<" qi"; else if(a[i]=='8') cout<<" ba"; else cout<<" jiu"; } } cout<<endl; }

應該是沒人看到的…首先控制空格完全沒必要這樣…

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

using namespace std;
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  char a[1000];
  cin>>a;
  for(int i=0;a[i]!='\0';i++){
    if(i)
        cout<<" ";
    if(a[i]=='-')
        cout<<"fu";
    else if(a[i]=='0')
        cout<<"ling";
    else if(a[i]=='1')
        cout<<"yi";
    else if(a[i]=='2')
        cout<<"er";
    else if(a[i]=='3')
        cout<<"san";
    else if(a[i]=='4')
        cout<<"si";
    else if(a[i]=='5')
        cout<<"wu";
    else if(a[i]=='6')
        cout<<"liu";
    else if(a[i]=='7')
        cout<<"qi";
    else if(a[i]=='8')
        cout<<"ba";
    else 
        cout<<"jiu";
  }
  cout<<endl;  
}

咔擦一下直接優化了一半,還能繼續優化

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

using namespace std;
typedef long long ll;
string s[11]= {"ling","yi","er","san","si","wu","liu","qi","ba","jiu","fu"};
int main() {
    ios::sync_with_stdio(false);
    char a[1000];
    cin>>a;
    for(int i=0; a[i]!='\0'; i++) {
        //cout<<"hello"<<endl;
        if(i)
            cout<<" ";
        if(a[i]=='-')
            cout<<s[10];
        else
            cout<<s[a[i]-'0'];
    }
    cout<<endl;
}

沒的說,寫部落格,看自己以前的想法 果然自己是真的長進了….233333