pat 1082. Read Number in Chinese (25)

来源:互联网 发布:淘宝虚假交易怎么恢复 编辑:程序博客网 时间:2024/05/17 06:55

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
32000 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

http://pat.zju.edu.cn/contests/pat-a-practise/1082

对于这类题目,我这种比较水的程序员感觉很棘手。到现在也没有想出一种比较快的解决方法。自己可以把代码写出来,不过代码行基本上是别人的两倍,
除了对于常量数组的处理有点笨之外,自己的逻辑也是比较死的逻辑,出现很多if else 语句 当中有相似的代码,却不知道如何优化。对于很多空格,还要一个个慢慢调整。
下面有两位代码写的比较好的博主,列出来。
第一位博主的文章,我看懂了,第二位还没细看。

http://blog.csdn.net/stephen_wong/article/details/34636917
http://blog.csdn.net/xyzchenzd/article/details/36385567

自己的代码:

//9:23 -> reading ->thinking ->10:05 coding finished 10:39#include <stdio.h>#include <string.h>char cn[13];int num[13];void printfNum(int n){  if(n==0)  {    printf("ling");  }else if(n==1)  {    printf("yi");  }else if(n==2)  {    printf("er");  }else if(n==3)  {    printf("san");  }else if(n==4)  {    printf("si");  }else if(n==5)  {    printf("wu");  }else if(n==6)  {    printf("liu");  }else if(n==7)  {    printf("qi");  }else if(n==8)  {    printf("ba");  }else if(n==9)  {    printf("jiu");  }}void printfPow(int i){    if(i==3)    {      printf(" Qian");    }else if(i==2)    {      printf(" Bai");    }else if(i==1)    {      printf(" Shi");    }}int main(){  //-100000000,100000000,-123456789  //  //freopen("pat1082test.txt","r",stdin);  scanf("%s",cn);  int positive = 0,i=0,len,j=0,tmp=0;  len = strlen(cn);  if(cn[0]=='-')  {    printf("Fu ");    positive = 1;    for(i=0;i<len;i++)    {      cn[i] = cn[i+1];    }    len--;  }  j = 0;  for(i=len-1;i>=0;i--)  {    num[len-i-1] = cn[i]-'0';  }  if(len==1)  {    printfNum(num[0]);    return 0;  }  int isZero = 0,hasNum = 0,hasWan = 0;  for(i=len-1;i>=0;i--)  {    if(i==8)    {      printfNum(num[i]);      printf(" Yi");      hasNum = 1;    }else if(i>=4&&i<=7)    {      if(i==7)      {        if(num[i]==0)        {          isZero = 1;        }else        {          if(hasNum==1)          {            printf(" ");          }          printfNum(num[i]);          printf(" Qian");          hasWan = 1;        }      }else if(i>=5&&i<=6)      {        if(num[i]==0)        {          isZero = 1;        }else        {          if(isZero==1)          {            printf(" ling ");            isZero = 0;          }else          {            if(hasWan||hasNum)            {              printf(" ");            }          }          printfNum(num[i]);          printfPow(i-4);          hasWan = 1;        }      }else if(i==4)      {        if(num[i]!=0)        {          if(hasWan || hasNum)          {            printf(" ");          }          printfNum(num[i]);          printf(" Wan");          isZero = 0;        }else        {          if(hasWan!=0)          {            printf(" Wan");          }        }              }    }else if(i<=3&&i>=0)    {      if(num[i]==0)      {        isZero = 1;      }else      {        if(len>i+1)        {          if(isZero)          {            printf(" ling ");            isZero = 0;          }else          {            printf(" ");          }        }        printfNum(num[i]);        printfPow(i);      }    }  }  printf("\n");  return 0;}




0 0
原创粉丝点击