蓝桥杯 算法提高 9-3摩尔斯电码(Java解题)
来源:互联网 发布:孟加拉海关数据 编辑:程序博客网 时间:2024/04/30 21:54
问题描述
摩尔斯电码破译。类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文。请不要使用"zylib.h",只能使用标准库函数。用' * '表示' . ',中间空格用' | '表示,只转化字符表。
摩尔斯码定义见:http://baike.baidu.com/view/84585.htm?fromId=253988。
摩尔斯码定义见:http://baike.baidu.com/view/84585.htm?fromId=253988。
提示
清橙进行评测时,输入是以EOF结尾的,而不是换行符。(EOF不是一个字符,“以EOF结尾”是一种通俗但不严谨的说法。)因此可以通过以下方式之一获取输入:
1. 一次读入整行字符串,再进行后续解析。
2. 使用getchar或scanf一次读入一个字符,通过它们的返回值判断输入结束。
1. 一次读入整行字符串,再进行后续解析。
2. 使用getchar或scanf一次读入一个字符,通过它们的返回值判断输入结束。
样例输出
代码:
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner s = new Scanner(System.in);String str = s.next();int end = 0, start = 0;String str2;while (true) {end = str.indexOf("|", start);// 寻找|存在的位置if (end < 0)// 最后结束时|不在字符串末尾,返回值为负str2 = str.substring(start);// 将字符串进行分割,从start开始一直到最后elsestr2 = str.substring(start, end);// 将字符串进行分割,从start开始一直到end结束start = end + 1;// 寻找下一个|/** 判断代码并转换 */if (str2.equals("*-"))System.out.print("a");else if (str2.equals("-***"))System.out.print("b");else if (str2.equals("-*-*"))System.out.print("c");else if (str2.equals("-**"))System.out.print("d");else if (str2.equals("*"))System.out.print("e");else if (str2.equals("**-*"))System.out.print("f");else if (str2.equals("--*"))System.out.print("g");else if (str2.equals("****"))System.out.print("h");else if (str2.equals("**"))System.out.print("i");else if (str2.equals("*---"))System.out.print("j");else if (str2.equals("-*-"))System.out.print("k");else if (str2.equals("*-**"))System.out.print("l");else if (str2.equals("--"))System.out.print("m");else if (str2.equals("-*"))System.out.print("n");else if (str2.equals("---"))System.out.print("o");else if (str2.equals("*--*"))System.out.print("p");else if (str2.equals("**-"))System.out.print("q");else if (str2.equals("***-"))System.out.print("r");else if (str2.equals("***"))System.out.print("s");else if (str2.equals("-"))System.out.print("t");else if (str2.equals("**-"))System.out.print("u");else if (str2.equals("***-"))System.out.print("v");else if (str2.equals("*--"))System.out.print("w");else if (str2.equals("-**-"))System.out.print("x");else if (str2.equals("-*--"))System.out.print("y");else if (str2.equals("--**"))System.out.print("z");if (end < 0)break;// 当遍历到最后end返回值为负时退出}}}
解析:
stringObject.indexOf(int ch,int fromIndex):从fromIndex指定位置开始,获取ch在字符串中出现的位置
stringObject.substring(start,stop)一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start
substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符。
如果参数 start 与 stop 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。如果 start 比stop 大,那么该方法在提取子串之前会先交换这两个参数。
注意:substring() 不接受负的参数
阅读全文
0 0
- 蓝桥杯 算法提高 9-3摩尔斯电码(Java解题)
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 算法提高 9-3摩尔斯电码
- 蓝桥杯算法提高 9-3摩尔斯电码
- 蓝桥杯 ADV-226 算法提高 9-3摩尔斯电码
- 蓝桥杯 算法提高 9-3摩尔斯电码 ADV-226
- 算法提高 9-3摩尔斯电码 map
- 蓝桥杯-9-3摩尔斯电码(java)
- 蓝桥杯摩尔斯电码
- 摩尔斯电码
- 摩尔斯电码
- 摩尔斯电码
- 摩尔斯电码 — 摩尔斯密码
- java从小白到工程师--基础篇(二)
- 惩罚函数
- Mastering C- Concurrency.pdf 英文原版 免费下载
- Vuejs中关于computed、methods、watch的区别
- MyEclipse修改pageEncoding默认编码
- 蓝桥杯 算法提高 9-3摩尔斯电码(Java解题)
- 自定义-流式布局
- SQLHelper层对数据库的访问代码
- redis补充
- 调整数组使奇数全部都位于偶数前面。 杨氏矩阵
- 浅析JavaScript中this储存
- Adapter的作用及用法(四种适配器详解)
- 数据库字段char、varchar和nvarchar的区别
- C++ Concurrency in Action.pdf 英文原版 免费下载