PAT甲级 1082 Read Number in Chinese 把阿拉伯数字翻译成中文

PAT甲级 1082 Read Number in Chinese 把阿拉伯数字翻译成中文
PAT甲级 1082 Read Number in Chinese 把阿拉伯数字翻译成中文

Solution:

这道题模拟得太烦了。。。。
特别的结尾的空格控制,还有“ling”的输出。。最终还是过了

代码如下:

//把阿拉伯数字翻译为中文 模拟
#include<iostream>
#include<string>
#include<math.h>
using namespace std;

long n;
string s;
string num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int flag[9];

string to_string(int a){
    string s="";
    while(a!=0){
        char ch=a%10+'0';
        s=ch+s;
        a=a/10;
    }
    return s;
}

int main(){
    cin>>n;
    if(n<0){
        s=to_string(-n);
    }else{
        s=to_string(n);
    }
    int len=s.length();
    for(int i=len-1;i>=0;i--){
        flag[8-i]=s[len-i-1]-'0';
    }

    if(n<0){
        cout<<"Fu ";
    }
    if(flag[0]!=0){//说明是亿
        cout<<num[flag[0]]<<" Yi ";
        if(flag[1]!=0||flag[2]!=0||flag[3]!=0||flag[4]!=0){//说明万位不为0,要输出万
            if(flag[5]!=0||flag[6]!=0||flag[7]!=0||flag[8]!=0){//说明不是整万,要输出后面的
                if(flag[1]){cout<<num[flag[1]]<<" Qian";if(flag[2]||flag[3]||flag[4]){cout<<" ";}}else{cout<<num[0]<<" ";}
                if(flag[2]){cout<<num[flag[2]]<<" Bai";if(flag[3]||flag[4]){cout<<" ";}}else if(flag[1]!=0&&flag[2]==0&&(flag[3]!=0||flag[4]!=0)){cout<<num[0]<<" ";}
                if(flag[3]){cout<<num[flag[3]]<<" Shi";if(flag[4]){cout<<" ";}}else if(flag[2]!=0&&flag[3]==0&&flag[4]!=0){cout<<num[0]<<" ";}
                if(flag[4]){cout<<num[flag[4]];}
                cout<<" Wan ";
                if(flag[5]){cout<<num[flag[5]]<<" Qian";if(flag[6]||flag[7]||flag[8]){cout<<" ";}}else{cout<<num[0]<<" ";}
                if(flag[6]){cout<<num[flag[6]]<<" Bai";if(flag[7]||flag[8]){cout<<" ";}}else if(flag[5]!=0&&flag[6]==0&&(flag[7]!=0||flag[8]!=0)){cout<<num[0]<<" ";}
                if(flag[7]){cout<<num[flag[7]]<<" Shi";if(flag[8]){cout<<" ";}}else if(flag[6]!=0&&flag[7]==0&&flag[8]!=0){cout<<num[0]<<" ";}
                if(flag[8]){cout<<num[flag[8]];}
            }else{
                if(flag[1]){cout<<num[flag[1]]<<" Qian";if(flag[2]||flag[3]||flag[4]){cout<<" ";}}else{cout<<num[0]<<" ";}
                if(flag[2]){cout<<num[flag[2]]<<" Bai";if(flag[3]||flag[4]){cout<<" ";}}else if(flag[1]!=0&&flag[2]==0&&(flag[3]!=0||flag[4]!=0)){cout<<num[0]<<" ";}
                if(flag[3]){cout<<num[flag[3]]<<" Shi";if(flag[4]){cout<<" ";}}else if(flag[2]!=0&&flag[3]==0&&flag[4]!=0){cout<<num[0]<<" ";}
                if(flag[4]){cout<<num[flag[4]]<<" ";}
                cout<<" Wan";
            }
        }else{
            if(flag[5]!=0||flag[6]!=0||flag[7]!=0||flag[8]!=0){//说明不是整万,要输出后面的
                cout<<num[0]<<" ";
                if(flag[5]){cout<<num[flag[5]]<<" Qian";if(flag[6]||flag[7]||flag[8]){cout<<" ";}}
                if(flag[6]){cout<<num[flag[6]]<<" Bai";if(flag[7]||flag[8]){cout<<" ";}}else if(flag[5]!=0&&flag[6]==0&&(flag[7]!=0||flag[8]!=0)){cout<<num[0]<<" ";}
                if(flag[7]){cout<<num[flag[7]]<<" Shi";if(flag[8]){cout<<" ";}}else if(flag[6]!=0&&flag[7]==0&&flag[8]!=0){cout<<num[0]<<" ";}
                if(flag[8]){cout<<num[flag[8]];}
            }else{
                cout<<num[flag[0]]<<" Yi";
            }
        }
    }else{
        if(flag[1]!=0||flag[2]!=0||flag[3]!=0||flag[4]!=0){//说明万位不为0,要输出万
            if(flag[5]!=0||flag[6]!=0||flag[7]!=0||flag[8]!=0){//说明不是整万,要输出后面的
                if(flag[1]){cout<<num[flag[1]]<<" Qian";if(flag[2]||flag[3]||flag[4]){cout<<" ";}}
                if(flag[2]){cout<<num[flag[2]]<<" Bai";if(flag[3]||flag[4]){cout<<" ";}}else if(flag[1]!=0&&flag[2]==0&&(flag[3]!=0||flag[4]!=0)){cout<<num[0]<<" ";}
                if(flag[3]){cout<<num[flag[3]]<<" Shi";if(flag[4]){cout<<" ";}}else if(flag[2]!=0&&flag[3]==0&&flag[4]!=0){cout<<num[0]<<" ";}
                if(flag[4]){cout<<num[flag[4]];}
                cout<<" Wan ";
                if(flag[5]){cout<<num[flag[5]]<<" Qian";if(flag[6]||flag[7]||flag[8]){cout<<" ";}}else{cout<<num[0]<<" ";}
                if(flag[6]){cout<<num[flag[6]]<<" Bai";if(flag[7]||flag[8]){cout<<" ";}}else if(flag[5]!=0&&flag[6]==0&&(flag[7]!=0||flag[8]!=0)){cout<<num[0]<<" ";}
                if(flag[7]){cout<<num[flag[7]]<<" Shi";if(flag[8]){cout<<" ";}}else if(flag[6]!=0&&flag[7]==0&&flag[8]!=0){cout<<num[0]<<" ";}
                if(flag[8]){cout<<num[flag[8]];}
            }else{
                if(flag[1]){cout<<num[flag[1]]<<" Qian";if(flag[2]||flag[3]||flag[4]){cout<<" ";}}
                if(flag[2]){cout<<num[flag[2]]<<" Bai";if(flag[3]||flag[4]){cout<<" ";}}else if(flag[1]!=0&&flag[2]==0&&(flag[3]!=0||flag[4]!=0)){cout<<num[0]<<" ";}
                if(flag[3]){cout<<num[flag[3]]<<" Shi";if(flag[4]){cout<<" ";}}else if(flag[2]!=0&&flag[3]==0&&flag[4]!=0){cout<<num[0]<<" ";}
                if(flag[4]){cout<<num[flag[4]]<<" ";}
                cout<<" Wan";
            }
        }else{
            if(flag[5]!=0||flag[6]!=0||flag[7]!=0||flag[8]!=0){//说明不是整万,要输出后面的
                if(flag[5]){cout<<num[flag[5]]<<" Qian";if(flag[6]||flag[7]||flag[8]){cout<<" ";}}
                if(flag[6]){cout<<num[flag[6]]<<" Bai";if(flag[7]||flag[8]){cout<<" ";}}else if(flag[5]!=0&&flag[6]==0&&(flag[7]!=0||flag[8]!=0)){cout<<num[0]<<" ";}
                if(flag[7]){cout<<num[flag[7]]<<" Shi";if(flag[8]){cout<<" ";}}else if(flag[6]!=0&&flag[7]==0&&flag[8]!=0){cout<<num[0]<<" ";}
                if(flag[8]){cout<<num[flag[8]];}
            }else{
                cout<<num[flag[0]];
            }
        }
    }
    return 0;
}

上一篇:PTA 1082 射击比赛 (c++)


下一篇:python/shell脚本报异常^M: bad interpreter: No such file or directory