poj2121

字符串处理,还是用string类比较容易,先将字符串用million和thousand切分成三段,然后用同样的方法处理这三段即可。

注意输入数据结尾有多余空行

ContractedBlock.gif ExpandedBlockStart.gif View Code
 
   
#include < iostream >
#include
< cstdlib >
#include
< cstring >
#include
< cstdio >
using namespace std;

string name[] =
{
" 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 " };
int id[] =
{
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };

int getid( string st)
{
for ( int i = 0 ; i < 27 ; i ++ )
if (st == name[i])
return id[i];
return - 1 ;
}

int toint( string st)
{
if (st == " zero " )
return 0 ;
int ret = 0 ;
int pos;
if ((pos = st.find( " hundred " )) != string ::npos)
{
ret
+= getid(st.substr( 0 , pos - 1 )) * 100 ;
if (st.length() >= pos + 8 )
st.erase(
0 , pos + 8 );
else
st.erase(
0 , pos + 7 );
}
if ((pos = st.find( " " )) != string ::npos)
{
ret
+= getid(st.substr( 0 , pos));
if (st.length() >= pos + 1 )
st.erase(
0 , pos + 1 );
else
st.erase(
0 , pos);
}
if (st.length() > 0 )
ret
+= getid(st);
return ret;
}

int main()
{
// freopen("D:\\t.txt", "r", stdin);
string st;
while (getline(cin, st) && st != "" )
{
int ans = 0 ;
int pos;
int ne = 1 ;
if (st.substr( 0 , 8 ) == " negative " )
{
ne
= - 1 ;
st.erase(
0 , 9 );
}
if ((pos = st.find( " million " )) != string ::npos)
{
ans
+= 1000000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 8 )
st.erase(
0 , pos + 8 );
else
st.erase(
0 , pos + 7 );
}
if ((pos = st.find( " thousand " )) != string ::npos)
{
ans
+= 1000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 9 )
st.erase(
0 , pos + 9 );
else
st.erase(
0 , pos + 8 );
}
if (st.length() > 0 )
ans
+= toint(st);
ans
*= ne;
printf(
" %d\n " , ans);
}
return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值