PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)

简介: PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)

题目链接:点击打开链接

题目大意:智能读数。

解题思路:

if 一位数:

else:

 if 数位不为0:

   if 第一位为1,则shi开头而不是yi shi开头的情况。

   else。

 else:

   if 10亿 && 第二位为0。

   else if 多个连续0 && 不在万位 && 不在最后两位。

   else if 万位为0:

      if 1亿情况,到万位之前都是0,进入后继续判断该位的下一位是否为0(避免与连续0的0重复起来),pb("ling")。

      else if 10亿情况,到万位之前都是0,进入后继续判断该位的下一位是否为0(避免与连续0的0重复起来),pb("ling")。

      else pb("wan")。

AC 代码

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
vector<string> vs;
int main()
{
    string unit[11]={"","","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi","Shi"},
            num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
    char c;
    string s;
    cin>>s;
    int fu=s[0]=='-'?1:0;
    if(fu) s.erase(0,1), printf("Fu ");
    int len=s.length();
    if(len==1) vs.push_back(num[s[0]-'0']);
    else
    {
        for(int i=0;i<len;i++)
        {
            c=s[i];
            if(c!='0')
            {
//                if(i==0 && c=='1' && unit[len-i]=="Shi") vs.push_back(unit[len-i]);
//                else vs.push_back(num[c-'0']), vs.push_back(unit[len-i]);
                vs.push_back(num[c-'0']), vs.push_back(unit[len-i]);
            }
            else
            {
                if(len==10 && i==1) vs.push_back("Yi");
                else if(i<len-1 && i!=len-5 && s[i+1]!='0')
                    vs.push_back(num[c-'0']);
                else if(len>5 && i==len-5)
                {
                    if(len==9 && s[1]=='0' && s[2]=='0' && s[3]=='0')
                    {
                        if(s[5]!='0') vs.push_back(num[c-'0']);
                    }
                    else if(len==10 && s[1]=='0' && s[2]=='0' && s[3]=='0' && s[4]=='0')
                    {
                        if(s[6]!='0') vs.push_back(num[c-'0']);
                    }
                    else vs.push_back("Wan");
                }
            }
        }
    }
    printf("%s",vs[0].c_str());
    for(int i=1;i<vs.size();i++) if(vs[i]!="") printf(" %s",vs[i].c_str());
    puts("");
    return 0;
}
目录
相关文章
|
10月前
|
C++
【PAT甲级 - C++题解】1117 Eddington Number
【PAT甲级 - C++题解】1117 Eddington Number
49 0
|
10月前
|
C++
【PAT甲级 - C++题解】1038 Recover the Smallest Number
【PAT甲级 - C++题解】1038 Recover the Smallest Number
42 1
|
10月前
|
C++ 容器
【PAT甲级 - C++题解】1144 The Missing Number
【PAT甲级 - C++题解】1144 The Missing Number
40 0
|
10月前
|
存储 人工智能 C++
【PAT甲级 - C++题解】1104 Sum of Number Segments
【PAT甲级 - C++题解】1104 Sum of Number Segments
52 0
|
10月前
|
人工智能 文件存储 C++
【PAT甲级 - C++题解】1019 General Palindromic Number
【PAT甲级 - C++题解】1019 General Palindromic Number
54 0
|
10月前
|
C++
【PAT甲级 - C++题解】1024 Palindromic Number
【PAT甲级 - C++题解】1024 Palindromic Number
26 0
|
10月前
|
C++
【PAT甲级 - C++题解】1082 Read Number in Chinese
【PAT甲级 - C++题解】1082 Read Number in Chinese
62 0
|
C++
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
PAT (Advanced Level) Practice - 1038 Recover the Smallest Number(30 分)
99 0
|
5月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
63 1
|
5月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
21 0