zoukankan      html  css  js  c++  java
  • PAT甲级——【牛客练习题1002】

    题目描述

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way.  Output "Fu" first if it is negative.  For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu".  Note: zero ("ling") must be handled correctly according to the Chinese tradition.  For example, 100800 is "yi Shi Wan ling ba Bai".

    输入描述:

    Each input file contains one test case, which gives an integer with no more than 9 digits.



    输出描述:

    For each test case, print in a line the Chinese way of reading the number.  The characters are separated by a space and there must be no extra space at the end of the line.

    输入例子:

    -123456789

    输出例子:

    Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     vector<string> level = { "Fu","Shi","Bai","Qian" };
    11     vector<string> Wei = { "","Wan","Yi" };
    12     vector<string> numbers = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
    13     vector<string> res;
    14     string Num;
    15     cin >> Num;
    16     if(Num[0] == '-')//如果是负数
    17     {
    18         res.push_back(level[0]);
    19         Num.erase(0, 1);
    20     }
    21     int n = Num.length();    
    22     if (n == 1)//如果只有一位,则直接输出即可并结束
    23     {
    24         cout << numbers[Num[0] - '0'] << endl;
    25         return 0;
    26     }
    27     int f = 0;
    28     for (int i = 0; i < n; ++i)
    29     {
    30         int a = Num[i] - '0';//取出数字
    31         int p = (n - i - 1) % 4;//判断是否是4位间隔
    32         if (a > 0)
    33         {
    34             if (f)//中间有零存在
    35             {
    36                 res.push_back(numbers[0]);
    37                 f = 0;
    38             }
    39             res.push_back(numbers[a]);//输入数字
    40             if (p > 0)//不是各位
    41                 res.push_back(level[p]);//输入位
    42         }
    43         else if (p != 0)//当中间有0且不是0不是在个位上
    44             f = 1;
    45         if (p == 0 && res[res.size() - 1] != "Yi")//是4位间隔且中间不是全为0,例如100000004,就不用输出wan
    46             res.push_back(Wei[(n - i) / 4]);
    47     }
    48     for (int i = 0; i < res.size() - 1; ++i)
    49         cout << res[i] << " ";
    50     cout << res[res.size() - 1] << endl;//最后一位不用输出空格
    51     return 0;
    52 }
  • 相关阅读:
    hdu 1250 简单大整数加法
    hdu 3594 强连通好题仙人掌图,对自己的tarjan模板改下用这个
    hdu 1568关于斐波那契数列的公式及其思维技巧
    hdu 1245 Saving James Bond 策画几何+最短路 最短路求步数最少的路径
    关于c对文件的操作
    hdu 1829 带权并查集的运用类似于食物链但是更简单些
    hdu 1716 深搜dfs
    hdu 1713求分数的最小公倍数
    1268 和为K的组合 Meet in mid二分思路
    1163 最高的奖励 贪心 + 并查集
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11143884.html
Copyright © 2011-2022 走看看