Translating pinyin into Arabic numerals and Pinyin Arabic numerals
Input a string of Pinyin characters and output the Arabic numerals (Ling, Yi, Er, San, Si, Wu, Liu, Qi, Ba, and Jiu). The number cannot exceed 100,000.
For ease of processing, the input must be a digital unit. For example, 10 is YiShi, not a separate Shi.
Enter jiuwanjiuqianjiubaijiu
99999 output
Basic Idea: special processing of Wan, Qian, Bai, and Shi as separators. The rest are common numbers.
#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;}
Test cases:
Babaibashba
Jiuwanjiuqianjiubaijiu
YiShi
Result: