hdoj-1228-A+B

来源:互联网 发布:西方记者知乎 编辑:程序博客网 时间:2024/06/03 10:47

Problem Description
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
 

Input
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
 

Output
对每个测试用例输出1行,即A+B的值.
 

Sample Input
one + two =three four + five six =zero seven + eight nine =zero + zero =
 

Sample Output
39096

中文题面,意思很好懂。

直接模拟过去就好了

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;    int main()      {          char str[30],s1[20];          int i,j,len,s,sum1,sum2,flag;          while(gets(str)!=NULL)          {              len=strlen(str);              sum1=sum2=0;               flag=0;               for(i=0,j=0;i<len;i++)              {                  if(str[i]>='a'&&str[i]<='z')                       s1[j++]=str[i];                  else if(str[i]==' '&&str[i-1]!='+')                  {                      s1[j]='\0';                     if(!strcmp(s1,"zero")) s=0;                       else if(!strcmp(s1,"one")) s=1;                      else if(!strcmp(s1,"two")) s=2;                      else if(!strcmp(s1,"three")) s=3;                      else if(!strcmp(s1,"four")) s=4;                      else if(!strcmp(s1,"five")) s=5;                      else if(!strcmp(s1,"six")) s=6;                      else if(!strcmp(s1,"seven")) s=7;                      else if(!strcmp(s1,"eight")) s=8;                      else if(!strcmp(s1,"nine")) s=9;                      j=0;                       if(!flag)                          sum1=sum1*10+s;                      else                           sum2=sum2*10+s;                   }                  else if(str[i]=='+')                      flag=1;               }              if(sum1==0&&sum2==0)                  break;               else                  printf("%d\n",sum1+sum2);          }          return 0;      }  


0 0