zoukankan      html  css  js  c++  java
  • 1005. Spell It Right (20)

    原题连接:https://www.patest.cn/contests/pat-a-practise/1005

    这道题我是将读入的数处理后进行相加,然后统计总数的位数,然后将总数不断的取余、模10,每次余数入栈,最后进行输出。

    总的来说我的思路以及方法还是稍有繁琐,尤其是输出那块。代码如下:

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<stdbool.h>
      4 typedef struct SNode{
      5     int Data;
      6     struct SNode *Next;
      7 }Stack;
      8 
      9 Stack *CreatStack()
     10 {
     11     Stack *S=malloc(sizeof(struct SNode));
     12     S->Next=NULL;
     13     return S;
     14 }
     15 
     16 void Push(Stack *S,int X)
     17 {
     18     Stack *cell=malloc(sizeof(struct SNode));
     19     cell->Data=X;
     20     cell->Next=S->Next;
     21     S->Next=cell;
     22 }
     23 bool IsEmpty(Stack *S)
     24 {
     25     return (S->Next==NULL);
     26 }
     27 int  Pop(Stack *S)
     28 {
     29     if (!IsEmpty(S)){
     30     int X;
     31     Stack*Tmpcell=malloc(sizeof (struct SNode));
     32     Tmpcell=S->Next;
     33     X=Tmpcell->Data;
     34     S->Next=Tmpcell->Next;
     35     free(Tmpcell);
     36     return X;}
     37 }
     38 
     39 int main()
     40 {
     41     char c;
     42     int  s=0;
     43     int X,Data;
     44     Stack *S;
     45     S=CreatStack();
     46 
     47     while ((c=getchar())!='
    ')
     48     {
     49         s+=c-'0';
     50     }
     51 
     52     int tmp=s;
     53     int cnt=1;
     54     while((tmp/=10))cnt++;
     55 
     56     while(cnt--){
     57     X=s%10;
     58     Push(S,X);
     59     s/=10;
     60     }
     61 
     62     int flag=0;
     63     while (!IsEmpty(S))
     64     {
     65         Data=Pop(S);
     66         switch (Data)
     67         {
     68             case 1:
     69             {
     70                 if (!flag){printf("one");flag=1;}
     71                 else printf(" one");
     72             }break;
     73             case 2:
     74             {
     75                 if (!flag){printf("two");flag=1;}
     76                 else printf(" two");
     77             }break;
     78             case 3:
     79             {
     80                 if (!flag){printf("three");flag=1;}
     81                 else printf(" tree");
     82             }break;
     83             case 4:
     84             {
     85                 if (!flag){printf("four");flag=1;}
     86                 else printf(" four");
     87             }break;
     88             case 5:
     89             {
     90                 if (!flag){printf("five");flag=1;}
     91                 else printf(" five");
     92             }break;
     93             case 6:
     94             {
     95                 if (!flag){printf("six");flag=1;}
     96                 else printf(" six");
     97             }break;
     98             case 7:
     99             {
    100                 if (!flag){printf("seven");flag=1;}
    101                 else printf(" seven");
    102             }break;
    103             case 8:
    104             {
    105                 if (!flag){printf("eight");flag=1;}
    106                 else printf(" eight");
    107             }break;
    108             case 9:
    109             {
    110                 if (!flag){printf("nine");flag=1;}
    111                 else printf(" nine");
    112             }break;
    113             case 0:
    114             {
    115                 if (!flag){printf("zero");flag=1;}
    116                 else printf(" zero");
    117             }break;
    118         }
    119     }
    120     return 0;
    121 }
    View Code

    然后参考了他人的代码,别的不说,单就输出那块用了hash映射是我需要学习和掌握的地方:

     1 #include<stdio.h>
     2 int main()
     3 {
     4     char str[10][6]={"zero", "one", "two", "three", "four", "five", "six",
     5         "seven", "eight", "nine"};
     6     int arr[5];
     7     int index=0;
     8 
     9     char ch;
    10     int sum=0;
    11     while (1)
    12     {
    13         scanf("%c",&ch);
    14         if (ch>='0' && ch<='9')sum+=(ch-'0');
    15         else if (ch=='
    ')break;
    16     }
    17     if (sum==0){printf("zero");return 0;}
    18     while(sum)
    19     {
    20         arr[index++]=sum%10;
    21         sum/=10;
    22     }
    23     int i;
    24     for(i=index-1;i>=0;i--)
    25     {
    26         printf("%s",str[arr[i]]);
    27         if (i)printf(" ");
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    Python基础学习九 数据库备份
    Python基础学习八 写日志
    Python 小练习三 发邮件
    Python基础补充(二) 多核CPU上python多线程并行的一个假象【转】
    pat 1118 Birds in Forest (25分) 并查集
    Java Map实现按value从大到小排序
    java实现排列组合(通俗易懂)
    java实现24点游戏代码
    eclipse搭建struts2环境及所遇到的问题
    java非常好用的读取文件的流的代码
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6298463.html
Copyright © 2011-2022 走看看