Give Me the Number(map)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2971

View Code
 1 #include<iostream>
 2 #include<map>
 3 #include<string.h>
 4 using namespace std ;
 5 map<string, int>a ;
 6 string s ;
 7 void init()
 8 {
 9     a["zero"] = 0 ;
10     a["one"] = 1 ;
11     a["two"] = 2 ;
12     a["three"] = 3 ;
13     a["four"] = 4 ;
14     a["five"] = 5 ;
15     a["six"] = 6 ;
16     a["seven"] = 7 ;
17     a["eight"] = 8 ;
18     a["nine"] = 9 ;
19     a["ten"]=10;
20     a["eleven"]=11;
21     a["twelve"]=12;
22     a["thirteen"]=13;
23     a["fourteen"]=14;
24     a["fifteen"]=15;
25     a["sixteen"]=16;
26     a["seventeen"]=17;
27     a["eighteen"]=18;
28     a["nineteen"]=19;
29     a["twenty"]=20;
30     a["thirty"]=30;
31     a["forty"]=40;
32     a["fifty"]=50;
33     a["sixty"]=60;
34     a["seventy"]=70;
35     a["eighty"]=80;
36     a["ninety"]=90;
37 }
38 int main()
39 {
40     int t, i ;
41     init() ;
42     cin>>t ;
43     cin.get() ;
44     while(t--)
45     {
46         getline(cin, s, '\n') ;
47         s += ' ' ;
48         int len = s.length() ;
49         int st = 0 ;
50         int sum = 0 ;
51         int num = 0 ;
52         for(i=0; i<len; i++)
53         {
54             if(s[i]==' ')
55             {
56                 string str = s.substr(st, i-st) ;
57                 st = i + 1 ;
58                 if(str=="and")
59                 continue ;
60                 else
61                 if(str=="million")
62                 {
63                     num *= 1000000 ;
64                     sum += num ;
65                     num = 0 ;
66                 }
67                 else
68                 if(str=="thousand")
69                 {
70                     num *= 1000 ;
71                     sum += num ;
72                     num = 0 ;
73                 }
74                 else
75                 if(str=="hundred")
76                 {
77                      num *= 100 ;
78                 }
79                 else
80                 num += a[str] ;
81             }
82         }
83         sum += num ;
84         cout<<sum<<endl ;
85     }
86     return 0 ;
87 }

C、C++中字符输入:

http://blog.csdn.net/xuezhongfenfei/article/details/8472460

posted @ 2013-03-16 19:48  yelan@yelan  阅读(236)  评论(0编辑  收藏  举报