poj2121--暴力解法

#include<iostream>
#include<string>
using namespace std;
string sum[50]={"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"};
int main(){
    string s;
    int num=0,res=0;
    while(cin>>s){
        if(s==sum[0])
        cout<<"-";
        else if(s==sum[1])
        num+=0;
        else if(s==sum[2])
        num+=1;
        else if(s==sum[3])
        num+=2;
        else if(s==sum[4])
        num+=3;
        else if(s==sum[5])
        num+=4;
        else if(s==sum[6])
        num+=5;
        else if(s==sum[7])
        num+=6;
        else if(s==sum[8])
        num+=7;
        else if(s==sum[9])
        num+=8;
        else if(s==sum[10])
        num+=9;
        else if(s==sum[11])
        num+=10;
        else if(s==sum[12])
        num+=11;
        else if(s==sum[13])
        num+=12;
        else if(s==sum[14])
        num+=13;
        else if(s==sum[15])
        num+=14;
        else if(s==sum[16])
        num+=15;
        else if(s==sum[17])
        num+=16;
        else if(s==sum[18])
        num+=17;
        else if(s==sum[19])
        num+=18;
        else if(s==sum[20])
        num+=19;
        else if(s==sum[21])
        num+=20;
        else if(s==sum[22])
        num+=30;
        else if(s==sum[23])
        num+=40;
        else if(s==sum[24])
        num+=50;
        else if(s==sum[25])
        num+=60;
        else if(s==sum[26])
        num+=70;
        else if(s==sum[27])
        num+=80;
        else if(s==sum[28])
        num+=90;
        else if(s==sum[29]){
            num*=100;
        }
        else if(s==sum[30]){//当为1000之后,原数应加上num*1000,而非直接乘,1000000也是一样 
            res+=num*1000;
            num=0;
        }
        else if(s==sum[31]){
            res+=num*1000000;
            num=0;
        }
        char biao=getchar();
        //当系统读到回车时,输出 
        if(biao=='\n'){
            cout<<num+res<<endl;
            num=0;res=0;
        }
    }
    return 0;
}

 

posted on 2015-10-23 20:30  T~Z  阅读(531)  评论(0编辑  收藏  举报