1. 程式人生 > >PAT程式設計考題——甲級1082( Read Number in Chinese ) C++實現

PAT程式設計考題——甲級1082( Read Number in Chinese ) C++實現

#include<iostream>
#include<math.h>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<vector>
using namespace std;
#define INF 100000000
#define maxn 100010
int store[maxn];
int num;
string a[100]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string b[100]={"","Shi","Bai","Qian"};
string c[4]={"","Wan","Yi"};
void fun(int num)//處理小於10000的數 反思列舉出所有情況可能更簡單
{
int temp=num;
 int store2[4]={0};//為了方便後續處理0不輸出
 int j=0;
 while(num){
  store2[j++]=num%10;
  num=num/10;
 }
 j--;
 int i;
 int z;
 for( z=0;z<4;z++)
 if(store2[z]!=0)   break;
 for( i=j;i>=0;i--)//從後往前第一不為零
 {
  if(store2[i]!=0) break;
 }
int time=0;//空格同樣容易出錯
if(temp%1000==0&&temp!=0) cout<<a[temp/1000]<<" qian";
else  {
for(int k=i;k>=z;k--){
    if(store2[k]==0&&time==0&&z!=i) cout<<"ling ",time++;//中間連續多個零
 else{
 if(store2[k]!=0&&k!=0) cout<<a[store2[k]]<<" "<<b[k];
 if(k==0&&store2[k]!=0) cout<<a[store2[k]];
 if(k!=z&&time!=1) cout<<" ";}
    }
}
}
int main()
{ cin>>num;
int temp=num;
if(num==0) {cout<<"ling";
return 0;}
if(num<0) cout<<"Fu ",num=-num;
int i=0;
while(num){
store[i++]=num%10000;
num=num/10000; 
}
i--;
int s,e;
for(s=0;s<4;s++)
if(store[s]!=0) break;
for(e=i;e>=0;e--)
{