PAT-A-1082. Read Number in Chinese (25)

来源:互联网 发布:淘宝上雪莲蛹虫草膏 编辑:程序博客网 时间:2024/05/07 02:05

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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".

Input Specification:

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

Output Specification:

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.

Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai

提交代

#include<iostream>
#include<cstdio>
#include<string>


using namespace std;


int flagfirst = 1;
string  s[10] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string  quan[3] = { "Qian", "Bai", "Shi" };
int a[5];
int b[5];




void Exchange(int a[])
{
int flag1, flag2;

for (int i = 1; i < 5; i++)
{
flag1 = flag2 = 0;
if (a[i] != 0)
{
if (flagfirst)
{
flagfirst = 0;
cout << s[a[i]];
}
else
cout << " " << s[a[i]];


if (i!=4)
cout <<" "<< quan[i - 1];
}
else
{
for (int j = 0; j < i; j++)
{
if (a[j] != 0)
flag1 = 1;
}


for (int j = i + 1; j < 5; j++)
{
if (a[j] != 0)
flag2 = 1;
}


if (flag1==0 || flag2==0)
{
}
else
{
if (a[i - 1] != 0)
cout << " ling";
}
}
}
}
int main()
{
int n;
cin >> n;


if (n == 0)
{
cout << "ling";
return 0;
}


if (n < 0)
{
n = -n;
if (flagfirst)
{
cout << "Fu";
flagfirst = 0;
}
}
if (n / 100000000 != 0)
{


a[0] = 1;
if (flagfirst)
{
flagfirst = 0;
cout << s[n / 100000000] << " Yi";
}
else
cout << " " << s[n / 100000000] << " Yi";
}



a[1] = n % 100000000 / 10000000;
a[2] = n % 10000000 / 1000000;
a[3] = n % 1000000 / 100000;
a[4] = n % 100000 / 10000;


Exchange(a);
if (n / 10000 != 0)
{
b[0] = 1;
cout << " Wan";
}


b[1] = n % 10000 / 1000;
b[2] = n % 1000 / 100;
b[3] = n % 100 / 10;
b[4] = n % 10;


Exchange(b);


system("pause");
return 0;
}

0 0
原创粉丝点击