您的位置:首页 > 其它

PAT-1082. Read Number in Chinese (25)(模拟)

2016-03-03 21:11 239 查看
4位4位的考虑,情况比较多,且要把中间结果存起来,最后输出,因为空格的原因。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;

char words[12][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "shi"};
char output[30][10];
int cnt = 0;

void read(int n)
{
int a, b, c, d;
a = n/1000;
b = (n-1000*a)/100;
c = (n-1000*a-100*b)/10;
d = n-1000*a-100*b-10*c;

if (a != 0)
{
strcpy(output[cnt++], words[a]);
strcpy(output[cnt++], "Qian");
}

if ((b == 0) && a != 0 &&(c != 0 || d != 0))
strcpy(output[cnt++], "ling");
else if (b != 0)
{
strcpy(output[cnt++], words[b]);
strcpy(output[cnt++], "Bai");
}
if (c == 0 && b!= 0 && d != 0)
strcpy(output[cnt++], "ling");
else if (c != 0)
{
strcpy(output[cnt++], words[c]);
strcpy(output[cnt++], "Shi");
}

if (d != 0)
strcpy(output[cnt++], words[d]);
}

int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int N = 0;
cin >> N;

if (N==0)
{
cout << "ling";
return 0;
}
if (N < 0)
{
strcpy(output[cnt++], "Fu");
N = -N;
}

int yi, wan, ge;
yi = N/100000000;
wan = (N-100000000*yi)/10000;
ge = N-100000000*yi - 10000*wan;

if (yi != 0)
{
read(yi);
strcpy(output[cnt++], "Yi");
}
if (wan != 0)
{
if (yi != 0 && wan < 1000)
strcpy(output[cnt++], "ling");
read(wan);
strcpy(output[cnt++], "Wan");
}
if (ge != 0)
{
if ((wan != 0 && ge < 1000) || (wan == 0 && yi != 0 && ge < 1000))
strcpy(output[cnt++], "ling");
read(ge);
}

for (int i = 0; i < cnt; i++)
{
cout << output[i];
if (i != cnt-1)
cout << " ";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: