问题描述 摩尔斯电码破译。类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文。请不要使用”zylib.h”,只能使用标准库函数。用’ * ‘表示’ . ‘,中间空格用’ | ‘表示,只转化字符表。
摩尔斯码定义见:http://baike.baidu.com/view/84585.htm?fromId=253988。 提示 清橙进行评测时,输入是以EOF结尾的,而不是换行符。(EOF不是一个字符,“以EOF结尾”是一种通俗但不严谨的说法。)因此可以通过以下方式之一获取输入:
1. 一次读入整行字符串,再进行后续解析。
2. 使用getchar或scanf一次读入一个字符,通过它们的返回值判断输入结束。 样例输出:
关于split方法的博客链接为:
代码语言:javascript复制import java.util.HashMap;
import java.util.Scanner;
public class Main {
static String[] mode = {"*-","-***","-*-*","-**","*","**-*","--*","****","**","*---","-*-","*-**","--","-*","---","*--*","--*-","*-*","**","-","**-","***-","*--","-**-","-*--","--**"};
/*
* Java中的split方法对于"|"的分割要进行转义,必须用"\|"代替"|"
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
HashMap<String, String>map = new HashMap<String, String>();
StringBuffer translation = new StringBuffer();
char[] word = {'a'};
for ( int i = 0 ; i < 26 ; i ){
map.put(mode[i], new String(word));
word[0] = (char) (word[0] 1);
}
String sentence = in.next();
String[] split = sentence.split("\|");
for ( int i = 0 ; i < split.length ; i ){
String string = map.get(split[i]);
translation.append(string);
}
System.out.println(translation.toString());
in.close();
}
}