Input is a string containing only pinyin, please output the corresponding number sequence. The conversion relationship is as follows: Description: Pinyin yi er san si wu Liu Qi ba jiu Arabic numerals 1 2 3 4 5 6 7 8 9 The input characters contain only lowercase letters, and all characters can match exactly
Run time limit: Unlimited memory limit: Unlimited input: One line of string, less than 1000 length output: One line character (number) string Sample input: Yiersansi sample output: 1234
Ideas: 1) < Pinyin, number > as key pair, save in HashMap 2) string loop, take out the number of the response from the HashMap
Java Solution: Import Java.util.hashmap;import Java.util.Scanner;
public class main{public static void Main (string[] args) {
Scanner cin=new Scanner (system.in); Hashmap<string, integer> hashmap=new hashmap<> (); Hashmap.put ("Yi", 1); Hashmap.put ("ER", 2); Hashmap.put ("San", 3); Hashmap.put ("Si", 4); Hashmap.put ("WU", 5); Hashmap.put ("Liu", 6); Hashmap.put ("Qi", 7); Hashmap.put ("Ba", 8); Hashmap.put ("JIU", 9);
While
(Cin.hasnext ()) {
func (HashMap, Cin.next ());
Break
;
}
} public static void Func (Hashmap<string, integer> HashMap, String str) {
int k1=0; int k2=2; System.out.println (Hashmap.get ("111")); while (K2<=str.length ()) {
if (Hashmap.get (Str.subsequence (K1, K2))!=null)
{
System.out.print (Hashmap.get (str.subsequence (K1, K2)));
}
else {
k2++;
if (Hashmap.get (Str.subsequence (K1, K2))!=null)
{
System.out.print (Hashmap.get (str.subsequence (K1, K2)));
}
}
K1=k2;
k2+=2;
} }
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The pinyin to the number of a question