ZOJ 2971-G - Give Me the Number-模拟

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14910

给出 数字的英文

翻译成阿拉伯数字

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
char tm[1005];
map<string,int> sb;
char word[1005];
string he="and";
map<string,int> shuzi;
	string ss[1005];
	
	int cun=0;
	int ans=0;
int main()
{
int  deal_num();
string million="million";
string one="one";
string two="two";
string three="three";
string four="four";
string five="five";
string six="six";
string seven="seven";
string eight="eight";
string nine="nine";
string ten="ten";
string eleven="eleven";
string twelve="twelve";
string thirteen="thirteen";
string fourteen="fourteen";
string fifteen="fifteen";
string sixteen="sixteen";
string seventeen="seventeen";
string eighteen="eighteen";
string nineteen="nineteen";
string twenty="twenty";
string thirty="thirty";
string forty="forty";
string fifty="fifty";
string sixty="sixty";
string seventy="seventy";
string eighty="eighty";
string ninety="ninety"; 
string thousand ="thousand";
string hundred  ="hundred";

 	sb.insert(make_pair(million,1));
 	sb.insert(make_pair(thousand,1));
 

shuzi.insert(make_pair(one,1));
shuzi.insert(make_pair(two,2));
shuzi.insert(make_pair(three,3));
shuzi.insert(make_pair(four,4));
shuzi.insert(make_pair(five,5));
shuzi.insert(make_pair(six,6));
shuzi.insert(make_pair(seven,7));
shuzi.insert(make_pair(eight,8));
shuzi.insert(make_pair(nine,9));
shuzi.insert(make_pair(ten,10));

shuzi.insert(make_pair(eleven,11));
shuzi.insert(make_pair(twelve,12));
shuzi.insert(make_pair(thirteen,13));
shuzi.insert(make_pair(fourteen,14));
shuzi.insert(make_pair(fifteen,15));
shuzi.insert(make_pair(sixteen,16));
shuzi.insert(make_pair(seventeen,17));
shuzi.insert(make_pair(eighteen,18));
shuzi.insert(make_pair(nineteen,19)); 
 

shuzi.insert(make_pair(twenty,20));
shuzi.insert(make_pair(thirty,30));
shuzi.insert(make_pair(forty,40));
shuzi.insert(make_pair(fifty,50));
shuzi.insert(make_pair(sixty,60));
shuzi.insert(make_pair(seventy,70));
shuzi.insert(make_pair(eighty,80));
shuzi.insert(make_pair(ninety,90)); 
shuzi.insert(make_pair(hundred,100));  
 
	
	int t;
	cin>>t;
	getchar();
	while(t--)
	{
		ans=0;
	
		gets(tm);
		int len=strlen(tm);
		int j=0;
		cun=0;
		int i;
	 
		int again=0;
		for (i=0;i<=len;i++)
		{
			if (tm[i]==' '||i==len)
			{
			
				word[j]=0;
				string tmp=word;
					if (i==len)
				{
					if (tmp!=million&&tmp!=thousand)
						again=1;
				}
				if (sb.find(tmp)!=sb.end())
				{
					if (tmp==million )
					ans+=deal_num()*1000000;
					else
						if (tmp==thousand)
							ans+=deal_num()*1000;
						else
							ans+=deal_num();

					 
					cun=0; 
				}
				else
				{
					ss[cun++]=word;  
				}
				j=0;
			}
			else
				word[j++]=tm[i];
		}
		
		if (again)
		ans+=deal_num();
		printf("%d\n",ans);


	} 
 
	
	return 0;
	
}

int cal(int i,int j)
{
	int k;
	int sum=0;
	for (k=i;k<=j;k++)
	{
	
		int num=shuzi[ss[k]];
		if (num==100)
			sum*=100; 
		else
			sum+=num; 
	}	
	return sum;
} 

int deal_num()
{
	int i;
	int sum=0;
	int has_and=0;
	int and_idx=0;
	for (i=0;i<cun;i++)
	{
		if (has_and==1)
		{
			if (ss[i]==he)
			{
				sum+=cal(and_idx+1,i-1);
				and_idx=i;
			}
			else
				if (i==cun-1)
				{
					sum+=cal(and_idx+1,cun-1); 
				}
				else
					continue;
		}
		if (ss[i]==he)
		{
			sum+=cal(0,i-1);
			has_and=1;
			and_idx=i;
		}  
	 
	}

	if (has_and==0)
	{
		sum+=cal(0,cun-1);
	}


	return sum;
}


你可能感兴趣的:(ZOJ 2971-G - Give Me the Number-模拟)