两道华为在线编程题
2017-07-06 22:49
1131 查看
为啥要写这个,挺郁闷的今天,和朋友一起在做华为在线编程题,够折腾的。第一题输出到底有没有空格,简直了,第二题映射表太长,第三题不记得了。
1、输出100至1000以内的水仙花数
2、假设电报中用(.)表示1,用中划线(-)表示0,点与中划线的序列,能够编译成一个二进制数(可以看做无符号数)。二进制数转换成整数,映射表对应英文字母。多个点、中划线间用#隔开(多个连续#号算作一个) 电报没有点、中划线只有#时电报内容为空,二进制数值超过映射表输出“ERROR” 具体映射标见代码中
也不知道有没有bug,只记得这些了,火速分享……
1、输出100至1000以内的水仙花数
public static void main(String[] args) { int a = 0, b = 0, c = 0; int count = 0; int sum = 0; for (int i = 100; i < 1000; i++) { a = i / 100; b = i / 10 % 10; c = i % 10; if (i == (a * a * a + b * b * b + c * c * c)) { count++; System.out.println("第" + count + "个水仙花数:" + i); sum = sum + i; } } System.out.println("水仙花数总和:" + sum); }
2、假设电报中用(.)表示1,用中划线(-)表示0,点与中划线的序列,能够编译成一个二进制数(可以看做无符号数)。二进制数转换成整数,映射表对应英文字母。多个点、中划线间用#隔开(多个连续#号算作一个) 电报没有点、中划线只有#时电报内容为空,二进制数值超过映射表输出“ERROR” 具体映射标见代码中
import java.util.HashMap; import java.util.Scanner; public class Decode { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); char[] sInput = s.toCharArray();// 输入电报字符数组 // 映射表 (不连续的 没规律 看一个输入一个) HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(0, "F"); map.put(1, "G"); map.put(2, "R"); map.put(3, "S"); map.put(4, "T"); map.put(5, "L"); map.put(11, "W"); map.put(12, "X"); map.put(13, "Y"); map.put(14, "Z"); map.put(15, "U"); map.put(16, "A"); map.put(22, "B"); map.put(23, "C"); map.put(24, "D"); map.put(25, "E"); map.put(30, "p"); map.put(31, "j"); map.put(26, "l"); map.put(27, "m"); map.put(28, "n"); map.put(29, "o"); map.put(41, "e"); map.put(42, "q"); map.put(37, "a"); map.put(38, "b"); map.put(39, "c"); map.put(40, "d"); map.put(48, "s"); map.put(49, "t"); map.put(50, "u"); map.put(51, "v"); int i = 0; int al = 0; int n = sInput.length; String[] a = new String ; while (i < n) {//解析输入电报 while (i < n && sInput[i] == '#') { i++; } while (i < n && sInput[i] != '#') { if (sInput[i] == '-' || sInput[i] == '.') { a[al] = a[al] + sInput[i]; } i++; } al++; } if (al == 1) { System.out.println(""); return; } int[] num = new int[al - 1]; for (i = 0; i < num.length; i++) { num[i] = toNum(a[i]);//转换成映射表数字 } String result = ""; for (i = 0; i < num.length; i++) { if (map.containsKey(num[i])) { result = result + map.get(num[i]); } else { System.out.println("ERROR"); return; } } System.out.println(result); } public static int toNum(String s) { int num = 0; int temp = 0; char[] chs = s.toCharArray(); if (chs[0] == '.') temp = 1; num = temp; for (int i = 1; i < chs.length; i++) { num = num * 2; if (chs[i] == '.') temp = 1; else temp = 0; num = num + temp; } return num; } }
也不知道有没有bug,只记得这些了,火速分享……
相关文章推荐
- 牛客网三道编程题(三 之华为机试在线训练(二)字符串反转、统计不同字符个数、删除字符串中出现最少的字符
- 牛客网三道编程题(四)华为机试在线训练(三)将数字逆序输出、将n个字符串按照字典顺序排序
- 2016腾讯实习生在线笔试的两道编程题
- 牛客网三道编程题(五) 之华为机试在线训练(四)进制转换、删除重复出现的数字、句子逆序
- 阿里秋招在线笔试两道编程题
- 2009年华为软件设计大赛两道编程题
- [C/C++]2009年华为软件设计大赛两道编程题
- 牛客网三道编程题(六) 之华为机试在线训练(五)对随机数字去重、排序;取近似值
- 华为的两道编程题
- 京东2015在线笔试编程题--分苹果Java实现
- Java在线笔试编程题(1)---list去除重复元素、幸运袋剪枝法、模拟洗牌
- 在线编程题-计算文本的 TFIDF值
- 华为2016研发工程师编程题 - 题解
- 百度2016研发工程师在线编程题 - 题解
- 删数--约瑟夫环-- 华为2016研发工程师编程题
- 华为在线训练(4)
- 华为[编程题]最高分是多少
- 牛客网几道编程题(二)之华为机试在线训练:计算字符个数、字符串最后一个单词的长度、分隔字符串
- 《华为机试在线训练》之汽水瓶
- 两道华为上机题目解答(Java语言)