1082 Read Number in Chinese (25 分)【难 / 模拟 字符串】

在这里插入图片描述
https://pintia.cn/problem-sets/994805342720868352/problems/994805385053978624
写的太垃圾,不想详细说,等有时间再写一个简洁的方法。
最讨厌的就是这个模拟题。
说一下我的处理空格的一些想法,直接用字符串连接,然后字符串流输出,这样可以处理多余的或少的空格。

#include<bits/stdc++.h>
using namespace std;
string s;
string a[10]={
    
    "ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string b[10]={
    
    "ling","","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
//              0    1   10    100    1000 10000
bool flag1,flag2,flag3,flag22,flag33;
string ans;
int main(void)
{
    
    
    cin>>s;
    if(s=="0") 
    {
    
    
        puts("ling");
        return 0;
    }
    if(s[0]=='-')  ans+="Fu ",s=s.substr(1);
    while(s.size()<9) s="0"+s;
    string temp=s.substr(0,1);
    if(temp[0]!='0') ans+=" "+a[temp[0]-'0']+" Yi ",flag1=1;
    temp=s.substr(1,4);
    bool index=0;
    flag22=flag1;
    for(int i=0,j=4;i<4;i++,j--)
    {
    
    
        int u=temp[i]-'0';
        if(!u) index=1;
        if(u&&index&&flag22&&temp[i-1]=='0') ans+=" ling ";
        if(u) ans+=" "+a[u]+" "+b[j]+" ",flag22=1,flag2=1;
    }
    if(flag2) ans+=" Wan ";
    temp=s.substr(5,4);
    index=0;
    flag33=flag22;
    for(int i=0,j=4;i<4;i++,j--)
    {
    
    
        int u=temp[i]-'0';
        if(!u) index=1;
        if(u&&index&&flag33&&temp[i-1]=='0') ans+=" ling ";
        if(u) ans+=" "+a[u]+" "+b[j]+" ",flag33=1;
    }
    string x; 
    stringstream l(ans);
    vector<string>ve;
    while(l>>x) ve.push_back(x);
    for(int i=0;i<ve.size();i++)
    {
    
    
        cout<<ve[i];
        if(i!=ve.size()-1) cout<<" ";
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/qq_46527915/article/details/121197699