POJ 2121 Inglish-Number Translator(暴力)

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for:
negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million
Input
The input consists of several instances. Notes on input:
Negative numbers will be preceded by the word negative.
The word “hundred” is not used when “thousand” could be. For example, 1500 is written “one thousand five hundred”, not “fifteen hundred”.

The input is terminated by an empty line.
Output
The answers are expected to be on separate lines with a newline after each.
Sample Input
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

Sample Output
6
-729
1000101
814022

解题思路:
不说了,直接暴力

#include<stdio.h>
#include<string.h>
char a[500];
char b[10][10];
int kk(char a[]) 
{
    
    
	int i,j=0,k=0;
	 for (i=0;a[i]!='\0';i++)
        {
    
    
            if (a[i]==' ')
            {
    
    
            	b[j][k++]='\0';
                j++;
                k=0;
            }
            else
            b[j][k++]=a[i];
        }
        b[j][k++]='\0';
    return j+1;
}
int main()
{
    
    
	
    while(gets(a))
    {
    
    
        int i,k=1,sum=0,ans=0;
		if (!strlen(a))
            break;
        for (i=0;i<kk(a);i++)
        {
    
    
            if (!strcmp(b[i],"negative"))
                k=-1;       
            else 
			if(!strcmp(b[i],"zero"))
                sum=sum+0;
            else 
			if(!strcmp(b[i],"one"))
                sum=sum+1;
            else 
			if(!strcmp(b[i],"two"))
                sum=sum+2;
            else 
			if(!strcmp(b[i],"three"))
                sum=sum+3;
            else 
			if(!strcmp(b[i],"four"))
                sum=sum+4;
            else 
			if(!strcmp(b[i],"five"))
                sum=sum+5;
            else 
			if(!strcmp(b[i],"six"))
                sum=sum+6;
            else 
			if(!strcmp(b[i],"seven"))
                sum=sum+7;
            else 
			if(!strcmp(b[i],"eight"))
                sum=sum+8;
            else 
			if(!strcmp(b[i],"nine"))
                sum=sum+9;
            else 
			if(!strcmp(b[i],"ten"))
                sum=sum+10;
            else 
			if(!strcmp(b[i],"eleven"))
                sum=sum+11;
            else 
			if(!strcmp(b[i],"twelve"))
                sum=sum+12;
            else 
			if(!strcmp(b[i],"thirteen"))
                sum=sum+13;
            else 
			if(!strcmp(b[i],"fourteen"))
                sum=sum+14;
            else 
			if(!strcmp(b[i],"fifteen"))
                sum=sum+15;
            else 
			if(!strcmp(b[i],"sixteen"))
                sum=sum+16;
            else 
			if(!strcmp(b[i],"seventeen"))
                sum=sum+17;
            else 
			if(!strcmp(b[i],"eighteen"))
                sum=sum+18;
            else 
			if(!strcmp(b[i],"nineteen"))
                sum=sum+19;
            else 
			if(!strcmp(b[i],"twenty"))
                sum=sum+20;
            else 
			if(!strcmp(b[i],"thirty"))
                sum=sum+30;
            else 
			if(!strcmp(b[i],"forty"))
                sum=sum+40;
            else 
			if(!strcmp(b[i],"fifty"))
                sum=sum+50;
            else 
			if(!strcmp(b[i],"sixty"))
                sum=sum+60;
            else 
			if(!strcmp(b[i],"seventy"))
                sum=sum+70;
            else 
			if(!strcmp(b[i],"eighty"))
                sum=sum+80;
            else 
			if(!strcmp(b[i],"ninety"))
                sum=sum+90;
            else 
			if(!strcmp(b[i],"hundred"))
                sum=sum*100;
            else 
			if(!strcmp(b[i],"thousand"))//注意当为1000之后,原数应加上num*1000,而非直接乘,1000000也是一样
            {
    
     
	            ans+=sum*1000; 
	            sum=0; 
            } 
            else 
			if(!strcmp(b[i],"million")) 
           {
    
     
	            ans+=sum*1000000; 
	            sum=0; 
           } 
        }
        printf("%d\n",(ans+sum)*k); 
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46703995/article/details/113095614
今日推荐