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 |
本人自己的解法,相当麻烦:
- #include<iostream>
- #include<string>
- #include<map>
- using namespace std;
- int pow(int x, int y)
- {
- int sum = x;
- for(int i=0; i<y; i++)
- {
- sum*=10;
- }
- return sum;
- }
- int main(int argc, char* argv[])
- {
- int i = 0;
- int len;
- int r=0;
- int l=0;
- char *ptr;
- int t[1000];
- string*s = new string[1000];
- map<string,int> m;
- map<int,string> m1;
- m["zero"] = 0;
- m["one"] = 1;
- m["two"] = 2;
- m["three"] = 3;
- m["four"] = 4;
- m["five"] = 5;
- m["six"] = 6;
- m["seven"] = 7;
- m["eight"] = 8;
- m["nine"] = 9;
- m1[0] = "zero" ;
- m1[1] = "one";
- m1[2] = "two";
- m1[3] = "three";
- m1[4] = "four";
- m1[5] = "five";
- m1[6] = "six";
- m1[7] = "seven";
- m1[8] = "eight";
- m1[9] = "nine";
- char in[100000];
- while(gets(in))
- {
- len = strlen(in)+1;
- ptr = strtok(in, " ");
- while(ptr)
- {
- if(ptr[0]!='+'&&ptr[0]!='=')
- {
- t[i] = m[ptr];
- i++;
- }
- else
- {
- for(int k=i-1,f=0; k>=0; k--,f++)
- {
- r+=pow(t[f],k);
- }
- i=0;
- }
- ptr = strtok(NULL," ");
- }
- if(r == 0)
- return 0;
- int temp = r;
- int count = 0;
- while(r)
- {
- s[count++]=m1[r%10];
- r = r/10;
- l++;
- }
- for(int p=count-1; p>=0; p--)
- {
- cout<<s[p];
- if(p>0)
- cout<<" ";
- }
- cout<<endl;
- l=0;
- r=0;
- }
- return 0;
- }
- /*在其中学会了如何使用一个函数:
- 原型:extern char *strtok(char *s, char *delim);
- 用法:#include <string.h>
- 功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
- 说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
- strtok在s中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
- 返回指向下一个标记串。当没有标记串时则返回空字符NULL。
- 举例:
- // strtok.c
- #include <syslib.h>
- #include <string.h>
- #include <stdio.h>
- main()
- {
- char *s="Golden Global View";
- char *d=" ";
- char *p;
- clrscr();
- p=strtok(s,d);
- while(p)
- {
- printf("%s/n",s);
- strtok(NULL,d);
- }
- getchar();
- return 0;
- }*/