pat 1082. Read Number in Chinese (25)

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 语句 当中有相似的代码,却不知道如何优化。对于很多空格,还要一个个慢慢调整。
下面有两位代码写的比较好的博主,列出来。
第一位博主的文章,我看懂了,第二位还没细看。


自己的代码:

//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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值