这是一道模拟题。
前置知识
思路
- 当读取到了
thousand
和million
时,计数器要乘上对应的值并累加到最终答案里,并且把计数器归零(因为该数级已经计算完了)。 - 当读取到
hundred
时,只要计数器乘上 \(100\)。 - 否则如果是其他数,则直接累加到计数器即可。
AC code
#include<bits/stdc++.h>
using namespace std;
string s,k;
string addnumber[30]={"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"};
int add[30]={0,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 ans,sum;
int main(){
while(getline(cin,s)){
k="";
int len=s.size();
for(int i=len-1;i>0;i--){ //去行尾空格。不加会有玄学错误。
if(s[i]==' ') len--;
else break;
}
for(int i=0;i<len;i++){
if(s[i]!=' '&&i!=len-1){
k+=s[i];
continue;
}
if(i==len-1) k+=s[i];
if(k=="negative") cout<<'-';
for(int j=0;j<30;j++){
if(k==addnumber[j]){
sum+=add[j];
break;
}
}
if(k=="hundred") sum*=100;
if(k=="thousand") {
sum*=1000;
ans+=sum;
sum=0;
}
if(k=="million"){
sum*=1000000;
ans+=sum;
sum=0;
}
k="";
}
ans+=sum; //最后还要把剩下的数(个级)加上,不然像 114514 会输出成 110000。
cout<<ans<<endl;
sum=0,ans=0;
}
return 0;
}
标签:int,题解,30,计数器,len,Translator,UVA486
From: https://www.cnblogs.com/bubble-sort/p/18369956