PAT刷题记录

PAT1082 Read Number in Chinese

坑太多了,调了一个多小时,最后发现tm 0的时候没有特判,艹

#include<iostream>
#include<cstring>
using namespace std;
string num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string c1[5]={" ","Wan","Shi","Bai","Qian"};
string c2[5]={" "," ","Shi","Bai","Qian"};
int main()
{
    string s;
    bool ff=false;
    cin>>s;
    if(s=="0")
    {
        cout<<"ling";
        return 0;
    }
    if(s[0]=='-') 
    {
    cout<<"Fu";
    ff=true;
    s = s.erase(0,1);
    }
    bool flag1=false;
    bool flag2=false;
    bool flag3=false;
    bool flag4=false;
    if(s.length()==9)
    {
        int t=s[0]-'0';
        if(ff)
        {
        cout<<" "<<num[t]<<" Yi";
        }
        else
        {
            ff=true;
            cout<<num[t]<<" Yi";
        }
        s = s.erase(0,1);
    }
    if(s.length()<=8&&s.length()>4)
    {
        flag4=true;
      int cnt=s.length()-4;
      string s1=s.substr(0,cnt);
      for(int i=0;i<cnt;i++)
      {
          int t=s[i]-'0';
          if(t==0&&!flag1&&stoi(s.substr(i,s1.length()-i))!=0)
          {
              flag1=true;
              cout<<" ling";
          }
          else if(t==0&&!flag1&&stoi(s.substr(i,s1.length()-i))==0)
          {
              cout<<" Wan"; 
              s=s.erase(0,cnt);
              flag3=true;
              break;
          }
          else if(t==0&&stoi(s.substr(i,s1.length()-i))!=0)
          {
              
          }
          else if(t==0&&stoi(s.substr(i,s1.length()-i))==0)
          {
              cout<<" Wan";
              s=s.erase(0,cnt);
              flag3=true;
              break;
          }
          else{
              if(ff)
              {
          cout<<" "<<num[t]<<" "<<c1[cnt-i];
              }
              else
              {
                  ff=true;
                  cout<<num[t]<<" "<<c1[cnt-i];
              }
          }
      }
      
      
    }
    if(!flag3&&flag4)s=s.erase(0,s.length()-4);
    if(s.length()<=4)
    {
        for(int i=0;i<s.length();i++)
        {
          int t=s[i]-'0';
          if(t==0&&!flag2&&stoi(s.substr(i,s.length()-i))!=0)
          {
              flag2=true;
              cout<<" ling";
          }
          else if(t==0&&flag2)
          {
              
          }
          else if(t==0&&flag2&&stoi(s.substr(i,s.length()-i))==0)
          {
              
          }
          else if(t==0&&!flag2&&stoi(s.substr(i,s.length()-i))==0)
          {
              return 0;
          }
          else if(t!=0&&i==s.length()-1){
            cout<<" "<<num[t];
          
          }
          else
          {
              if(ff)
              {
              cout<<" "<<num[t]<<" "<<c2[s.length()-i];
              }
              else
              {
                  ff=true;
                  
                  cout<<num[t]<<" "<<c2[s.length()-i];
              }
          }
        }
    }
    
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值