题解 | #牛牛的字符菱形#

学英语

http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

C:强行分类,你值得拥有

    1. #include <stdio.h>
    1. char first[19][10]={{"one"},{"two"},{"three"},{"four"},{"five"},{"six"},{"seven"},{"eight"},{"nine"},{"ten"},{"eleven"},{"twelve"},{"thirteen"},{"fourteen"},{"fifteen"},{"sixteen"},{"seventeen"},{"eighteen"},{"nineteen"}};
      
    1. char second[9][10]={{"ten"},{"twenty"},{"thirty"},{"forty"},{"fifty"},{"sixty"},{"seventy"},{"eighty"},{"ninety"}};
      
    1. char third[8]="hundred";
      
    1. char fourth[9]="thousand";
      
    1. char fifth[8]="million";
      
    1. void hundred(int n)
    1. {
    1. if(n>0 && n<1000)
      
    1. {
      
    1.     if(n>0 && n<20)
      
    1.         printf("%s",first[n-1]);
      
    1.     else if(n>19 && n<100)
      
    1.     {
      
    1.         printf("%s ",second[n/10-1]);
      
    1.         if(n%10!=0)
      
    1.             printf("%s",first[n%10-1]);
      
    1.     }
      
    1.     else
      
    1.     {
      
    1.         printf("%s %s ",first[(n/100)-1],third);
      
    1.         if((n%100)/10>1)
      
    1.         {
      
    1.             printf("and %s ",second[((n%100)/10)-1]);
      
    1.             if(n%10!=0)
      
    1.                 printf("%s ",first[n%10-1]);
      
    1.         }
      
    1.         else if((n%100)/10==1)
      
    1.             printf("and %s ",first[n%100-1]);
      
    1.         else if((n%100)/10==0)
      
    1.         {
      
    1.             if(n==100)
      
    1.                 printf("%s ",first[n%10-1]);
      
    1.             else
      
    1.                 printf("and %s ",first[n%10-1]);
      
    1.         }
      
    1.     }
      
    1. }
      
    1. }
    1. void thousand(int n)
    1. {
    1. if(n>999 && n<1000000)
      
    1. {
      
    1.     int i=n/1000;
      
    1.     if(i/100!=0)
      
    1.     {
      
    1.         printf("%s %s ",first[(i/100)-1],third);
      
    1.         if((i%100)/10>1)
      
    1.         {
      
    1.             printf("and %s ",second[((i%100)/10)-1]);
      
    1.             if(i%10!=0)
      
    1.                 printf("%s ",first[i%10-1]);
      
    1.         }
      
    1.         else if((i%100)/10==1)
      
    1.             printf("and %s ",first[i%100-1]);
      
    1.         else if((i%100)/10==0)
      
    1.             printf("and %s ",first[i%10-1]);
      
    1.     }
      
    1.     if(i/100==0)
      
    1.     {
      
    1.         if((i%100)/10>1)
      
    1.         {
      
    1.             printf("%s ",second[((i%100)/10)-1]);
      
    1.             if(i%10!=0)
      
    1.                 printf("%s ",first[i%10-1]);
      
    1.         }
      
    1.         else if((i%100)/10<=1)
      
    1.             printf("%s ",first[i%100-1]);
      
    1.     }
      
    1.     printf("%s ",fourth);
      
    1.     while(n>1000)
      
    1.         n-=1000;
      
    1.     hundred(n);
      
    1. }
      
    1. }
    1. void million(int n)
    1. {
    1. if(n>1000000)
      
    1. {
      
    1.     int i=n/1000000;
      
    1.     printf("%s %s ",first[i-1],fifth);
      
    1.     while(n>1000000)
      
    1.         n-=1000000;
      
    1.     thousand(n);
      
    1. }
      
    1. }
    1. int main()
    1. {
    1. long int n=0;
      
    1. scanf("%ld",&n);
      
    1. million(n);
      
    1. thousand(n);
      
    1. hundred(n);
      
    1. return 0;
      
    1. }
  • `
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务