Translating pinyin into Arabic numerals and Pinyin Arabic numerals

Source: Internet
Author: User

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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.