G - Give Me the Number 解题报告

G题,就是以前做过的字符串专题的差不多的题啊,就是多了个 “and ” 遇到 continue 即可。。。

 

#include<cstdio>
#include<cstring>
char text[35][20]={"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",// 0 ~ 10
 "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", // 11 ~ 20 
 "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", // 30 ~ 100 --- 21 ~ 28
 "thousand", "million", "negative","and"// 1000 --- 29 , 1000 000 --- 30 , '-' --- 31,'and'---32
}; 
//无脑流,模拟题

char ori[20000],s[100][20];

int find(char *ss){
	int i;
	for(i=0;i<33;i++)
		if(strcmp(text[i],ss)==0) break;
	if(i==31) return -1;
	else if(i==32) return -2;
	else if(i>=0 && i<=20) return i;
	else if(i>=21 && i<=28) return (i-20+2)*10;
	else if(i==29) return 1000;
	else if(i==30) return 1000000;
}

int main()
{
	int i,t;
    scanf("%d",&t);
	while(t){
        gets(ori); 
		if(strlen(ori)==0) continue;//没加这句居然会WA ,以后得注意加这句
		t--;
		long long ans=0;
		memset(s,0,sizeof(s));
		int tot=0,w=0;
		for(i=0;i<(int)strlen(ori);i++){
			if(ori[i]==' '){
				tot++;
				w=0; 
			}
			else s[tot][w++]=ori[i];
		}
		int thous=1,milli=1,hund=1;
		for(i=tot;i>=0;i--){
			int num=find(s[i]);
			if(num==-1) ans*=-1;
			else if(num==-2) continue;
			else if(num==1000000) {milli*=num;thous=1;hund=1;}
			else if(num==1000) {thous*=num;hund=1;}
			else if(num==100) hund*=num;
			else ans+=num*(milli*thous*hund);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值