poj 2121——Inglish-Number Translator

本文分享了一段C++代码,用于将英文表述的数字转换为阿拉伯数字,并详细介绍了实现方法,包括处理负数和千位分隔的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:将英文数字翻译为阿拉伯数字。囧!结束条件忘了判断,贡献两wa。

#include<iostream> #include<cstdio> #include<string> using namespace std; char to20[][20]={"zero", "one", "two", "three", "four", "five", "six","seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"}; char to100[][20]={"thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}; char tran[][20]={"hundred", "thousand", "million"}; char st[10000]; char ex[100][20]; int find20(char a[]) { for(int i=0;i<=20;i++) if(strcmp(a,to20[i])==0) return i; return -1; } int find100(char a[]) { for(int i=0;i<7;i++) if(strcmp(a,to100[i])==0) return (i+3)*10; return -1; } int main() { while(gets(st)!=NULL) { if(st[0]=='\0')break; bool fu=false; int i,len=strlen(st),cnt=0,j; for(i=0;i<len;i++) { j=0; while(st[i]!=' '&&i<len) { ex[cnt][j++]=st[i]; i++; } ex[cnt++][j++]='\0'; } int sum=0,star=0; if(strcmp("negative",ex[0])==0) { fu=true;star=1; } for(i=star;i<cnt;i++) { int subsum=0; while(i<cnt) { if(strcmp(ex[i],"hundred")==0)//遇一百的时候,不用break。 { subsum*=100; } if(strcmp(ex[i],"thousand")==0) { subsum*=1000; break; } if(strcmp(ex[i],"million")==0) { subsum*=1000000; break; } int k=find20(ex[i]); if(k!=-1) subsum+=k; k=find100(ex[i]); if(k!=-1) subsum+=k; i++; } sum+=subsum; } if(fu) sum*=-1; printf("%d\n",sum); } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值