ZOJ 2971

比赛时这道题没做出来,后来在网上搜了一种简单的方法,把0,1,2,。。。。20,30,。。90存到一个数组里,n1,n2,n3代表每三位的数,刚开始自动匹配,n3开始计算,遇到million,thousand,hundred,再乘以10^n最后把n1,n2,n3加在一起就是所求结果。

View Code
 1 #include<stdlib.h>
 2 #include<stdio.h>
 3 #include<string.h>
 4 
 5 char str[28][15]={"zero", "one", "two", "three", "four"
 6     , "five", "six", "seven", "eight", "nine"
 7     , "ten", "eleven", "twelve", "thirteen"
 8     , "fourteen", "fifteen", "sixteen", "seventeen"
 9     , "eighteen", "nineteen", "twenty", "thirty"
10     , "forty", "fifty", "sixty", "seventy"
11     , "eighty", "ninety"} ;
12 
13 int main()
14 {
15     int i, ncases, n1, n2, n3;
16     char c, s[15];
17         
18     scanf("%d",&ncases); 
19     while( ncases-- )
20     {
21        n1 = n2 = n3 = 0;            
22        while( 1)
23        {
24           scanf("%s",s);    
25           if(s[0] == 'm') 
26           { 
27              n1 = (n2 + n3)*1000000;
28              n2 = n3 = 0;   
29           } 
30           if(strcmp(s,"thousand") == 0) 
31           { 
32              n2 = n3*1000;
33              n3 = 0;
34           }  
35           if(strcmp(s,"hundred")==0) n3 *= 100; 
36            
37           for(i = 0; i < 20; i++)
38           {
39               if(strcmp(str[i],s) == 0) 
40               {      
41                   n3 += i;
42                   break;
43               }    
44           }
45            
46           for(i = 20; i<28; i++)
47           {
48              if(strcmp(str[i],s) == 0)
49              {
50                  n3 += (i-18)*10;
51                  break;
52              }
53           }       
54          c = getchar(); 
55          if(c == '\n') break; 
56        }
57        printf("%d\n",n1+n2+n3);   
58     
59     }
60     //system("pause");
61     return 0;
62 }
63     

 

你可能感兴趣的:(ZOJ)