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

来源:互联网 发布:怎样抢到淘宝秒杀产品 编辑:程序博客网 时间:2024/05/23 00:00
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;}

0 0
原创粉丝点击