HDU ACM 1228 A + B

http://acm.hdu.edu.cn/showproblem.php?pid=1228

 

只是A+B 输出结果,用了STL中的map 和 reverse()函数

 

//View Code
 1 #include <iostream>
 2 #include <map>
 3 #include <string>
 4 #include <algorithm>
 5 using namespace std;
 6 int main()
 7 {
 8     map <string , int > m;
 9     string str;
10     m["one"] = '1';
11     m["two"] = '2';
12     m["three"] = '3';
13     m["four"] = '4';
14     m["five"] = '5';
15     m["six"] = '6';
16     m["seven"] = '7';
17     m["eight"] = '8';
18     m["nine"] = '9';
19     m["zero"] = '0';
20     int mark = 0;
21     char str1[11000]={0};
22     char str2[11000]={0};
23     int num1=0,num2=0;
24     while(cin>>str)
25     {
26         if(str == "+")
27         {
28             mark = 1;
29         }
30         else
31         {
32             if(str == "=")
33             {
34                 if(str1[0] == '0' && str2[0] == '0' && strlen(str1) == 1 && strlen(str2) == 1)
35                 {
36                     return 0;
37                 }
38                 reverse(str1,str1+num1);
39                 reverse(str2,str2+num2);
40                 mark = 0;
41                 char str3[11000] = {0};
42                 int i;
43                 for(i=0;i<max(num1,num2);i++)
44                 {
45                     if(i >= min(num1,num2))
46                     {
47                         str3[i] = str1[i] + str2[i];
48                     }
49                     else
50                     {
51                         str3[i] = str1[i] - '0' + str2[i];
52                     }
53                 }
54                 int up = 0;
55                 for(i=0;i<max(num1,num2);i++)
56                 {
57                     str3[i] = str3[i] + up;
58                     if(str3[i] > '9')
59                     {
60                         str3[i] = str3[i] - '0';
61                         up = str3[i]/10;
62                         str3[i] = str3[i]%10;
63                         str3[i] = str3[i] + '0';
64                     }
65                     else
66                     {
67                         up = 0;
68                     }
69                 }
70                 if(up)
71                 {
72                     str3[i] = up + '0';
73                 }
74                 reverse(str3,str3+strlen(str3));
75                 cout<<str3<<endl;
76                 memset(str1,0,sizeof(str1));
77                 memset(str2,0,sizeof(str2));
78                 memset(str3,0,sizeof(str3));
79                 num1=0;
80                 num2=0;
81             }
82             else
83             {
84                 if(mark == 0)
85                 {
86                     str1[num1++] = m[str];
87                 }
88                 else
89                 {
90                     str2[num2++] = m[str];
91                 }
92             }
93         }
94     }
95     return 0;
96 }

 

posted @ 2012-09-08 19:57  zx雄  阅读(190)  评论(0编辑  收藏  举报