输入一串拼音,输出它代表的阿拉伯数字(Ling,Yi,Er,San,Si,Wu,Liu,Qi,Ba,Jiu),数字不会超过十万
为了处理方便。输入一定是数字带单位的,比如10会是YiShi,而不是一个单独的Shi.
输入 JiuWanJiuQianJiuBaiJiuShiJiu
输出 99999
基本思路:把Wan,Qian,Bai。Shi特殊处理,作为分隔符,其余的都是普通数字
#include<stdio.h> #include<string> #include<cctype> #include<map> #include<iostream> #include<vector> using namespace std; map<string,int>mm; vector<int>vec; vector<int>nums; void init() { mm["Ling"]=0; mm["Yi"]=1; mm["Er"]=2; mm["San"]=3; mm["Si"]=4; mm["Wu"]=5; mm["Liu"]=6; mm["Qi"]=7; mm["Ba"]=8; mm["Jiu"]=9; mm["Wan"]=10000; mm["Qian"]=1000; mm["Bai"]=100; mm["Shi"]=10; } int main(int argc, char *argv[]) { freopen("test.in","r",stdin); init(); string s; while(cin>>s){ nums.clear(); vec.clear(); int i=0; int j; while(1){ if(i<s.size()&&isupper(s[i])) { j=i; i++; while(i<s.size()&&islower(s[i])) ++i; string x; for(int k=j;k<i;++k) x+=s[k]; vec.push_back(mm[x]); } if(i==s.size())break; } int num=0; vector<int>::size_type ite=0; while(ite!=vec.size()){ if(vec[ite]>=10){ num*=vec[ite]; nums.push_back(num); num=0; } else num+=vec[ite]; ite++; } nums.push_back(num); int sum=0; for(int i=0;i<nums.size();++i) sum+=nums[i]; cout<<sum<<endl; } return 0; }測试用例:
BaBaiBaShiBa
JiuWanJiuQianJiuBaiJiuShiJiu
YiShi
结果为: