POJ 2121 模拟(注意:输入以空行结束)

题目链接:http://poj.org/problem?id=2121

题意:

就是让你把英文的的数字表示译成阿拉伯数字 

需要注意的是 

  1. The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred".
  2. 题目范围只有 -999,999,999~ 999,999,999 所以最大表示就只有   几百 百万.....

 

分析:

我的思路是用两个变量 ans 和 num 当遇到 hundred、thousand、million数量级时,判断后面的数量级是否比当前的数量级大,如果大那肯定是前面的数还要乘以后面的数量级,那么暂时就先放在num变量里,反之后面的数量级比当前的数量级小,则直接乘以当前的数量级之后加到ans变量里,并清空num  如果没有遇到这三个数量级,则直接加到num里即可。

需要注意的是,The input is terminated by an empty line. 输入一空行结束,所以多组输入  while(getline(cin,s)&&s!="") 这么写。

 

这题一开始思路有点错了 后来改正思路之后,写了出来,最后在多组输入结束的条件卡了一下 改了这个地方之后居然一下过了...我以为我的思路比较混乱 可能又会WA几次...看了别人的题解之后才知道 就是简单暴力模拟,

 

我的AC代码

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
int main()
{
//    freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    string s;
    map<string,int> mp;
    mp["zero"]=0;mp["one"]=1;mp["two"]=2;mp["three"]=3;
    mp["four"]=4;mp["five"]=5;mp["six"]=6;mp["seven"]=7;
    mp["eight"]=8;mp["nine"]=9;mp["ten"]=10;mp["eleven"]=11;
    mp["twelve"]=12;mp["thirteen"]=13;mp["fourteen"]=14;
    mp["fifteen"]=15;mp["sixteen"]=16;mp["seventeen"]=17;
    mp["eighteen"]=18;mp["nineteen"]=19;mp["twenty"]=20;
    mp["thirty"]=30;mp["forty"]=40;mp["fifty"]=50;mp["sixty"]=60;
    mp["seventy"]=70;mp["eighty"]=80;mp["ninety"]=90;
    mp["hundred"]=100;mp["thousand"]=1000;mp["million"]=1000000;
    
    while(getline(cin,s)&&s!="")///!!!注意一下输入以空行结束!
    {
        string a[1000];
        int t,kase=0;
        int p=0;
        t=s.find(' ');
        if(t==string::npos)
            {a[kase]=s;kase++;}
        else
        {
            while(t!=string::npos)
            {
                a[kase]=s.substr(p,t-p);
                p=t+1;kase++;
                t=s.find(' ',p);
            }
            a[kase]=s.substr(p);

        }
//        for(int i=0;i<=kase;i++)
//            cout<<a[i]<<"-";
        int flag=0;
        long long ans=0,num=0;
        int len=kase+10;
        int b[1050]={0};
        for(int i=0;i<=kase;i++)
        {
            if(mp[a[i]]>=100)
                b[i]=mp[a[i]];
        }
        for(int i=0;i<=kase;i++)
        {
            if(a[i]=="negative") {flag=1;continue;}
            if(mp[a[i]]>=100)
            {
                int j;
                for(j=i+1;j<len;j++)
                    if(b[j]>mp[a[i]])break;
                if(j==len)
                    {ans+=num*mp[a[i]];num=0;}
                else
                    num=num*mp[a[i]];
            }
            else
            {
                num+=mp[a[i]];
            }
        }
        if(flag)
            cout<<"-"<<ans+num<<endl;
        else
            cout<<ans+num<<endl;
    }
    return 0;
}

 

别人的代码:传送门

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s,t;
    int p,r,ans;
    while (getline(cin,s) && s!="") {
          ans=0;
          r=0;
          do {
             p=s.find(" ");
             t=s.substr(0,p);
             s=s.substr(p+1);
             if (t=="negative") cout << "-";
             else if (t=="million") {
                  ans+=r*1000000;
                  r=0;
             }
             else if (t=="thousand") {
                  ans+=r*1000;
                  r=0;
             }
             else if (t=="hundred") r*=100;
             else if (t=="zero") r=0;
             else if (t=="one") r+=1;
             else if (t=="two") r+=2;
             else if (t=="three") r+=3;
             else if (t=="four") r+=4;
             else if (t=="five") r+=5;
             else if (t=="six") r+=6;
             else if (t=="seven") r+=7;
             else if (t=="eight") r+=8;
             else if (t=="nine") r+=9;
             else if (t=="ten") r+=10;
             else if (t=="eleven") r+=11;
             else if (t=="twelve") r+=12;
             else if (t=="thirteen") r+=13;
             else if (t=="fourteen") r+=14;
             else if (t=="fifteen") r+=15;
             else if (t=="sixteen") r+=16;
             else if (t=="seventeen") r+=17;
             else if (t=="eighteen") r+=18;
             else if (t=="nineteen") r+=19;
             else if (t=="twenty") r+=20;
             else if (t=="thirty") r+=30;
             else if (t=="forty") r+=40;
             else if (t=="fifty") r+=50;
             else if (t=="sixty") r+=60;
             else if (t=="seventy") r+=70;
             else if (t=="eighty") r+=80;
             else if (t=="ninety") r+=90;
          }while (p!=-1);
          cout << ans+r << endl;
    }
    //system("pause");
    return 0;
}

 

 

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值