POJ 2121

http://poj.org/problem?id=2121

一道字符串的转换的题目。

题意:就是把那个英文数字翻译成中文。

思路:首先打表,然后把每一个单独的单词分离出来,在组合相加相乘。

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 struct trans{
 5     char eng[10];
 6     int num;
 7 }s[40];
 8
 9 int main(){
10 //    freopen("in.txt","r",stdin);
11     strcpy(s[0].eng,"zero"),      s[0].num = 0;
12     strcpy(s[1].eng,"one"),       s[1].num = 1;
13     strcpy(s[2].eng,"two"),       s[2].num = 2;
14     strcpy(s[3].eng,"three"),     s[3].num = 3;
15     strcpy(s[4].eng,"four"),      s[4].num = 4;
16     strcpy(s[5].eng,"five"),      s[5].num = 5;
17     strcpy(s[6].eng,"six"),       s[6].num = 6;
18     strcpy(s[7].eng,"seven"),     s[7].num = 7;
19     strcpy(s[8].eng,"eight"),     s[8].num = 8;
20     strcpy(s[9].eng,"nine"),      s[9].num = 9;
21     strcpy(s[10].eng,"ten"),      s[10].num= 10;
22     strcpy(s[11].eng,"eleven"),   s[11].num= 11;
23     strcpy(s[12].eng,"twelve"),   s[12].num= 12;
24     strcpy(s[13].eng,"thirteen"), s[13].num= 13;
25     strcpy(s[14].eng,"fourteen"), s[14].num= 14;
26     strcpy(s[15].eng,"fifteen"),  s[15].num= 15;
27     strcpy(s[16].eng,"sixteen"),  s[16].num= 16;
28     strcpy(s[17].eng,"seventeen"),s[17].num= 17;
29     strcpy(s[18].eng,"eighteen"), s[18].num= 18;
30     strcpy(s[19].eng,"nineteen"), s[19].num= 19;
31     strcpy(s[20].eng,"twenty"),   s[20].num= 20;
32     strcpy(s[21].eng,"thirty"),   s[21].num= 30;
33     strcpy(s[22].eng,"forty"),    s[22].num= 40;
34     strcpy(s[23].eng,"fifty"),    s[23].num= 50;
35     strcpy(s[24].eng,"sixty"),    s[24].num= 60;
36     strcpy(s[25].eng,"seventy"),  s[25].num= 70;
37     strcpy(s[26].eng,"eighty"),    s[26].num= 80;
38     strcpy(s[27].eng,"ninety" ),   s[27].num= 90;
39     strcpy(s[29].eng,"hundred"),  s[29].num= 100;
40     strcpy(s[30].eng,"thousand"), s[30].num= 1000;
41     strcpy(s[31].eng,"million"),  s[31].num= 1000000;
42     strcpy(s[28].eng,"negative"); s[28].num= -1;
43     char a[200];
44     while(gets(a))
45     {
46         if(strlen(a)==0) break;
47         char tmp[50]={0};
48         int ans=0,flog=0,sum=0,x=1;
49         int len=strlen(a);
50         for(int i=0,k=0;i<len;i++)
51         {
52             if(a[i]!=‘ ‘) tmp[k++]=a[i];
53             if(a[i]==‘ ‘||i==len-1){
54                 k=0;
55                 flog=1;
56                 for(int m=0;m<=31;m++){
57                     if(strcmp(s[m].eng,tmp)==0){
58                             if(m==28) {
59                                 x=-1;
60                                 continue;
61                             }
62                             if(m<=27) sum+=s[m].num;
63                             if(m==29) sum*=s[m].num;
64                             if(m>=30) {
65                                 ans+=sum*s[m].num;
66                                 sum=0;
67                             }
68                             break;
69                     }
70                 }
71             }
72             if(flog==1){
73                 memset(tmp,0,sizeof(tmp));
74                 flog=0;
75             }
76         }
77         printf("%d\n",(ans+sum)*x);
78     }
79     return 0;
80 }
时间: 2024-10-10 13:32:55

POJ 2121的相关文章

POJ 2121 Inglish-Number Translator

来源:http://poj.org/problem?id=2121 Inglish-Number Translator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4475   Accepted: 1747 Description In this problem, you will be given one or more integers in English. Your task is to translate t

字符串处理,Poj(2121)

题目链接:http://poj.org/problem?id=2121 差一点就WA哭了,主要是自己傻逼了. 思路: 遇到hundred,sum*100; 但是遇到thouthend,million,ans+=sum*(... ...),sum=0; 因为到了thouthend,million,后面肯定又是新的数字,跟前面的没关系了,sum=0; 而hundred,后面是可以再加数的,比如22,所以这里只要sum*=100; 注意: 这里经典的一招,杰哥教我的,要重置一下order,只需要ord

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

Poj 题目分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ题目分类(转)

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea