A + B Again

A + B Again
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 201, Accepted users: 182
Problem 10281 : No special judgement
Problem description
Reads in positive integers A and B,calculates A + B.Pay attention:A and B is composed by simple English word. [0-9]:"zero","one","two","three","four","five","six","seven","eight","nine";
Input
Standard input will contain multiple test cases.Each testcase occupies a line.the form is "A + B =",the neighboring two characters string has a blank space.When A and B is 0 at the same time ,inputs finished.You shouldn't output the result 0.
Output
For each testcase outputs 1 line,The result of A + B,by simple English word too.Each word is separated with a single space.There's no space after the last word.
Sample Input
one + two =
three four + five six =
zero + zero =
Sample Output
three
nine zero
Problem Source
HNU Contest 
Submit   Discuss   Judge Status  Problems  Ranklist 

本人自己的解法,相当麻烦:

 
  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. using namespace std;
  5. int pow(int x, int y)
  6. {
  7.      int sum = x;
  8.      for(int i=0; i<y; i++)
  9.      {
  10.          sum*=10;
  11.      }
  12.      return sum;
  13. }
  14. int main(int argc, char* argv[])
  15. {    
  16.     int i = 0;
  17.     int len;
  18.     int r=0;
  19.     int l=0;
  20.     char *ptr;
  21.     int t[1000];
  22.     string*s = new string[1000];
  23.     map<string,int> m;
  24.     map<int,string> m1;
  25.     m["zero"] = 0;
  26.     m["one"] = 1;
  27.     m["two"] = 2;
  28.     m["three"] = 3;
  29.     m["four"] = 4;
  30.     m["five"] = 5;
  31.     m["six"] = 6;
  32.     m["seven"] = 7;
  33.     m["eight"] = 8;
  34.     m["nine"] = 9;
  35.     m1[0] = "zero" ;
  36.     m1[1] = "one";
  37.     m1[2] = "two";
  38.     m1[3] = "three";
  39.     m1[4] = "four";
  40.     m1[5] = "five";
  41.     m1[6] = "six";
  42.     m1[7] = "seven";
  43.     m1[8] = "eight";
  44.     m1[9] = "nine";
  45.     
  46.     char in[100000];
  47.     while(gets(in))
  48.     {
  49.        len = strlen(in)+1;
  50.        ptr = strtok(in, " ");
  51.        while(ptr)
  52.        {
  53.          if(ptr[0]!='+'&&ptr[0]!='=')
  54.          {
  55.             t[i] = m[ptr];
  56.             i++;
  57.          }
  58.          else
  59.          {
  60.             
  61.              for(int k=i-1,f=0; k>=0; k--,f++)
  62.              {
  63.                  r+=pow(t[f],k);
  64.             
  65.              }
  66.             
  67.              i=0;
  68.          }
  69.          ptr = strtok(NULL," ");
  70.        }
  71.     
  72.        if(r == 0)
  73.            return 0;
  74.        int temp = r;
  75.        int count = 0;
  76.        while(r)
  77.        {
  78.           s[count++]=m1[r%10];
  79.           
  80.           r = r/10;
  81.           l++;
  82.         
  83.        } 
  84.        for(int p=count-1; p>=0; p--)
  85.        {
  86.            cout<<s[p];
  87.            if(p>0)
  88.               cout<<" ";
  89.        }
  90.        cout<<endl;
  91.         
  92.      
  93.        l=0;
  94.        r=0;
  95.       
  96.     }
  97.     
  98.     return 0;
  99. }
  100. /*在其中学会了如何使用一个函数:
  101. 原型:extern char *strtok(char *s, char *delim);
  102.         
  103.   用法:#include <string.h>
  104.   
  105.   功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
  106.   
  107.   说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
  108.         strtok在s中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
  109.         返回指向下一个标记串。当没有标记串时则返回空字符NULL。
  110.   
  111.   举例:
  112.       // strtok.c
  113.       
  114.       #include <syslib.h>
  115.       #include <string.h>
  116.       #include <stdio.h>
  117.       main()
  118.       {
  119.         char *s="Golden Global View";
  120.         char *d=" ";
  121.         char *p;
  122.         
  123.         clrscr();
  124.         
  125.         p=strtok(s,d);
  126.         while(p)
  127.         {
  128.           printf("%s/n",s);
  129.           strtok(NULL,d);
  130.         }
  131.         getchar();
  132.         return 0;
  133.       }*/
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值