华为od机试题1 真题
生活随笔
收集整理的這篇文章主要介紹了
华为od机试题1 真题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
華為od機試題 真題
- 86.射擊比賽成績排序
- 85.計算屏幕字母數量
- 84.組成最大數字
- 82.輸出字符串中最小數字
- 81.數字4的個數
- 80.整數排列
- 79.多條件排列
- 78.時間排序
以下題目附帶Java解法,是我個人寫的,不一定是標準答案,沒有真正的測試數據,只能說是我自己認為通過率100%,也不一定是最優解。如果有錯誤或是有更好的解法,請評論告訴我!!!
86.射擊比賽成績排序
給定一個射擊比賽成績單 包含多個選手若干次射擊的成績分數 請對每個選手按其最高三個分數之和進行降序排名 輸出降序排名后的選手id序列 條件如下 1. 一個選手可以有多個射擊成績的分數,且次序不固定 2. 如果一個選手成績少于3個,則認為選手的所有成績無效,排名忽略該選手 3. 如果選手的成績之和相等,則相等的選手按照其id降序排列輸入描述:輸入第一行一個整數N表示該場比賽總共進行了N次射擊產生N個成績分數2<=N<=100輸入第二行一個長度為N整數序列表示參與每次射擊的選手id0<=id<=99輸入第三行一個長度為N整數序列表示參與每次射擊選手對應的成績0<=成績<=100輸出描述:符合題設條件的降序排名后的選手ID序列示例一輸入:133,3,7,4,4,4,4,7,7,3,5,5,553,80,68,24,39,76,66,16,100,55,53,80,55輸出:5,3,7,4說明:該場射擊比賽進行了13次參賽的選手為{3,4,5,7}3號選手成績53,80,55 最高三個成績的和為1884號選手成績24,39,76,66 最高三個成績的和為1815號選手成績53,80,55 最高三個成績的和為1887號選手成績68,16,100 最高三個成績的和為184比較各個選手最高3個成績的和有3號=5號>7號>4號由于3號和5號成績相等 且id 5>3所以輸出5,3,7,4 // 射擊比賽成績排序 public static void test086() {Scanner sc = new Scanner(System.in);String line1 = sc.nextLine();String line2 = sc.nextLine();String line3 = sc.nextLine();String[] strings2 = line2.split(",");String[] strings3 = line3.split(",");// 用Map存儲,key存選手號,value用list存選手所有的成績Map<Integer, List<Integer>> map = new HashMap<>();for (int i = 0; i < Integer.parseInt(line1); i++) {if (map.containsKey(Integer.parseInt(strings2[i]))) {List<Integer> list = map.get(Integer.parseInt(strings2[i]));list.add(Integer.parseInt(strings3[i]));} else {List<Integer> list = new ArrayList<>();list.add(Integer.parseInt(strings3[i]));map.put(Integer.parseInt(strings2[i]), list);}}Map<Integer, Integer> map2 = new HashMap();// 遍歷Map,對選手成績進行篩選,用一個Map<Integer, Integer>來存選手號和最大總成績for (Integer key : map.keySet()) {List<Integer> list = map.get(key);int listSize = list.size();if (listSize >= 3) {Collections.sort(list);// 最大總成績int listAll = list.get(listSize - 1) + list.get(listSize - 2) + list.get(listSize - 3);map2.put(key, listAll);}}// 用于拼接輸出結果StringBuilder builder = new StringBuilder();// 通過stream流的形式進行排序 sorted:對集合進行排序 e1與e2比較,結果大于0互換位置 小于等于0不變Set<Map.Entry<Integer, Integer>> entries = map2.entrySet();entries.stream().sorted((e1, e2) -> {if (e1.getValue().equals(e2.getValue())) {// 分數相等,選手號大的在前return e2.getKey() - e1.getKey();} else {// 分數大的在前return e2.getValue() - e1.getValue();}}).forEach((e) -> {builder.append(e.getKey() + ",");});System.out.println(builder.substring(0, builder.length() - 1));}85.計算屏幕字母數量
有一個特殊的五鍵鍵盤 上面有A、Ctrl-C、Ctrl-X、Ctrl-V、Ctrl-A A鍵在屏幕上輸出一個字母A Ctrl-C將當前所選的字母復制到剪貼板 Ctrl-X將當前選擇的字母復制到剪貼板并清空所選擇的字母 Ctrl-V將當前剪貼板的字母輸出到屏幕 Ctrl-A選擇當前屏幕中所有字母 注意:1. 剪貼板初始為空2. 新的內容復制到剪貼板會覆蓋原有內容3. 當屏幕中沒有字母時,Ctrl-A無效4. 當沒有選擇字母時Ctrl-C、Ctrl-X無效5. 當有字母被選擇時A和Ctrl-V這兩個輸出功能的鍵,會先清空所選的字母再進行輸出給定一系列鍵盤輸入, 輸出最終屏幕上字母的數量輸入描述:輸入為一行為簡化解析用數字12345分別代替A、Ctrl-C、Ctrl-X、Ctrl-V、Ctrl-A的輸入數字用空格分割輸出描述:輸出一個數字為屏幕上字母的總數量示例一:輸入:1 1 1輸出:3示例二:輸入:1 1 5 1 5 2 4 4輸出:2 // 計算屏幕字母數量public static void test085() {Scanner sc = new Scanner(System.in);String line = sc.nextLine();String[] split = line.split(" ");// 屏幕內容int result = 0;// 剪切板內容int ctrl_c_x = 0;// ctrl_A選中的內容int ctrl_a = 0;for (int i = 0; i < split.length; i++) {if (1 == Integer.parseInt(split[i])) { // A// Ctrl-A 選中內容,輸入A則覆蓋,屏幕內容長度為1if (ctrl_a != 0) {result = 1;} else { // Ctrl-A 沒選中內容,輸入A屏幕內容長度加1result++;}ctrl_a = 0;} else if (2 == Integer.parseInt(split[i])) { // Ctrl-C// 當Ctrl-A 選中內容時,Ctrl-C才有效if (0 != ctrl_a) {// 剪切板內容等于ctrl_A選中的內容ctrl_c_x = ctrl_a;}} else if (3 == Integer.parseInt(split[i])) { // Ctrl-X// 當Ctrl-A 選中內容時,Ctrl-X才有效if (0 != ctrl_a) {// 剪切板內容等于ctrl_A選中的內容ctrl_c_x = ctrl_a;// Ctrl-X后屏幕內容長度為0result = 0;// Ctrl-X后ctrl_A選中內容長度為0ctrl_a = 0;}} else if (4 == Integer.parseInt(split[i])) { // Ctrl-V// 當剪切板有內容ctrl_v才有用 // PS:這里有個疑問:當剪切板為空時,使用Ctrl-V是用空覆蓋還是無效呢??我的處理是無效,我覺得這里題目描述的不是很清楚if (0 != ctrl_c_x) {// Ctrl-A 選中內容時if (ctrl_a != 0) {result = ctrl_c_x;ctrl_a = 0;} else if (ctrl_a == 0) { // Ctrl-A 沒選中內容時result += ctrl_c_x;}}} else if (5 == Integer.parseInt(split[i])) { // Ctrl-A// 選中屏幕所有內容ctrl_a = result;}}System.out.println(result);}84.組成最大數字
小組中每位都有一張卡片 卡片是6位以內的正整數 將卡片連起來可以組成多種數字 計算組成的最大數字輸入描述:","分割的多個正整數字符串不需要考慮非數字異常情況小組種最多25個人輸出描述:最大數字字符串示例一輸入22,221輸出22221示例二輸入4589,101,41425,9999輸出9999458941425101 // 組成最大數字public static void test0843() {Scanner sc = new Scanner(System.in);String line = sc.nextLine();String[] split = line.split(",");// 類似于冒泡排序,用雙層for循環,將最大的組合往前放for (int i = 0; i < split.length - 1; i++) {for (int j = i + 1; j < split.length; j++) {String res1 = split[i] + split[j];String res2 = split[j] + split[i];if (Integer.parseInt(res1) < Integer.parseInt(res2)) {String value = split[i];split[i] = split[j];split[j] = value;}}}for (int i = 0; i < split.length; i++) {System.out.print(split[i]);}}82.輸出字符串中最小數字
1.輸入字符串s輸出s中包含所有整數的最小和,說明:1字符串s只包含a~z,A~Z,+,-, 2.合法的整數包括正整數,一個或者多個0-9組成,如:0,2,3,002,102 3.負整數,負號開頭,數字部分由一個或者多個0-9組成,如-2,-012,-23,-00023輸入描述:包含數字的字符串輸出描述:所有整數的最小和 示例:輸入:bb1234aa輸出10輸入:bb12-34aa輸出:-31說明:1+2-(34)=-31 // 輸出字符串中最小數字public static void test082() {Scanner sc = new Scanner(System.in);String line = sc.nextLine();// 將字符串轉為字符數組char[] chars = line.toCharArray();// 累加結果int sum = 0;for (int i = 0; i < chars.length; i++) {char c = chars[i];if (c == '-') {int start = i;i++;while (i < chars.length && Character.isDigit(chars[i])) {i++;}// 截取負數String substring = line.substring(start, i);// 當substring = 1時只有一個負號if (substring.length() > 1) {sum += Integer.parseInt(substring);}}// 判斷字符是不是數字if (Character.isDigit(c)) {// 將字符數字轉為數字,當字符數字小于第二個參數時,返回-1sum += Character.digit(c, 10);}}System.out.println(sum);}81.數字4的個數
程序員小明打了一輛出租車去上班。出于職業敏感,他注意到這輛出租車的計費表有點問題,總是偏大。 出租車司機解釋說他不喜歡數字4,所以改裝了計費表,任何數字位置遇到數字4就直接跳過,其余功能都正常。比如:1. 23再多一塊錢就變為25;2. 39再多一塊錢變為50;3. 399再多一塊錢變為500;小明識破了司機的伎倆,準備利用自己的學識打敗司機的陰謀。給出計費表的表面讀數,返回實際產生的費用。輸入描述:只有一行,數字N,表示里程表的讀數。(1<=N<=888888888)。 輸出描述:一個數字,表示實際產生的費用。以回車結束。 示例1: 輸入5 輸出4 說明5表示計費表的表面讀數。表示實際產生的費用其實只有4塊錢。示例2: 輸入17 輸出15 說明17表示計費表的表面讀數。15表示實際產生的費用其實只有15塊錢。 示例3: 輸入100 輸出81 說明:100表示計費表的表面讀數,81表示實際產生的費用其實只有81塊錢 // 數字4的個數public static void test081(){Scanner sc = new Scanner(System.in);int start = sc.nextInt();// 數字中有4的個數int sum = 0;for (int i = 1; i <= start; i++) {// 將數字轉為字符串,判斷數字是否包含4if ((i+"").contains("4")) {// 累加sum++;}}System.out.println(start - sum);80.整數排列
給定參數n,從1到n會有n個整數:1,2,3,...,n,這n個數字共有n!種排列.按大小順序升序列出所有排列的情況,并一一標記,當n=3時,所有排列如下:"123" "132" "213" "231" "312" "321"給定n和k,返回第k個排列.輸入描述:輸入兩行,第一行為n,第二行為k,給定n的范圍是[1,9],給定k的范圍是[1,n!]。輸出描述:輸出排在第k位置的數字。實例1:輸入:33輸出:213說明3的排列有123,132,213...,那么第三位置就是213實例2:輸入22輸出:21說明2的排列有12,21,那么第二位置的為21 // 整數排列 這道題目前還不懂,稍后總結public static void test080(){Scanner in = new Scanner(System.in);int n = Integer.parseInt(in.nextLine());int k = Integer.parseInt(in.nextLine());StringBuilder sb = new StringBuilder();List<Integer> candidates = new ArrayList<>();int[] factorials = new int[n + 1];factorials[0] = 1;int fact = 1;for (int i = 1; i <= n; ++i) {candidates.add(i);fact *= i;factorials[i] = fact;}k -= 1;for (int i = n - 1; i >= 0; --i) {// 計算候選數字的indexint index = k / factorials[i];sb.append(candidates.remove(index));k -= index * factorials[i];}System.out.println(sb);}79.多條件排列
某學校舉行運動會,學生們按編號(1、2、3.....n)進行標識, 現需要按照身高由低到高排列, 對身高相同的人,按體重由輕到重排列, 對于身高體重都相同的人,維持原有的編號順序關系。 請輸出排列后的學生編號輸入描述:兩個序列,每個序列由N個正整數組成,(0<n<=100)。第一個序列中的數值代表身高,第二個序列中的數值代表體重,輸出描述:排列結果,每個數據都是原始序列中的學生編號,編號從1開始,實例一:輸入:4100 100 120 13040 30 60 50輸出:2134 // 多條件排列public static void test079() {Scanner sc = new Scanner(System.in);int len = Integer.parseInt(sc.nextLine());// 身高String line1 = sc.nextLine();// 體重String line2 = sc.nextLine();String[] split1 = line1.split(" ");String[] split2 = line2.split(" ");// map的key為編號,value為學生的身高、體重組成的listMap<Integer, List<Integer>> map = new HashMap<>();for (int i = 1; i <= len; i++) {List<Integer> list = new ArrayList<>();// 身高list.add(Integer.parseInt(split1[i - 1]));// 體重list.add(Integer.parseInt(split2[i - 1]));map.put(i, list);}Set<Map.Entry<Integer, List<Integer>>> entrySet = map.entrySet();// 通過stream流的形式進行排序 sorted:對集合進行排序 s1與s2比較,結果大于0互換位置 小于等于0不變entrySet.stream().sorted((s1, s2) -> {List<Integer> value1 = s1.getValue();List<Integer> value2 = s2.getValue();// 身高相等比體重return value1.get(0) == value2.get(0) ? value1.get(1) - value2.get(1) : value1.get(0) - value2.get(0);}).forEach((s) -> {System.out.print(s.getKey());});}78.時間排序
運維工程師采集到某產品線網運行一天產生的日志n條現需根據日志時間先后順序對日志進行排序日志時間格式為H:M:S.NH表示小時(0~23)M表示分鐘(0~59)S表示秒(0~59)N表示毫秒(0~999)時間可能并沒有補全也就是說01:01:01.001也可能表示為1:1:1.1輸入描述第一行輸入一個整數n表示日志條數1<=n<=100000接下來n行輸入n個時間輸出描述按時間升序排序之后的時間如果有兩個時間表示的時間相同則保持輸入順序示例:輸入:201:41:8.91:1:09.211輸出1:1:09.21101:41:8.9示例輸入323:41:08.0231:1:09.21108:01:22.0輸出1:1:09.21108:01:22.023:41:08.023示例輸入222:41:08.02322:41:08.23輸出22:41:08.02322:41:08.23時間相同保持輸入順序 // 時間排序public static void test078() {Scanner sc = new Scanner(System.in);// 輸入的個數int len = Integer.parseInt(sc.nextLine());List<String> list = new ArrayList<>();int i = 0;while (i < len) {list.add(sc.nextLine());i++;}sc.close();// 對集合進行排序 a1與a2比較,結果大于0互換位置 小于等于0不變list.stream().sorted((a1, a2) -> {// 根據規則切分出時 分 秒 毫秒String[] time1 = a1.split(":");String[] time2 = a2.split(":");// 用.進行分割時需要用到轉義字符 \\String[] s_n1 = time1[2].split("\\.");String[] s_n2 = time2[2].split("\\.");int h1 = Integer.parseInt(time1[0]);int h2 = Integer.parseInt(time2[0]);int m1 = Integer.parseInt(time1[1]);int m2 = Integer.parseInt(time2[1]);int s1 = Integer.parseInt(s_n1[0]);int s2 = Integer.parseInt(s_n2[0]);int n1 = Integer.parseInt(s_n1[1]);int n2 = Integer.parseInt(s_n2[1]);// 小時不相等比較小時if (h1 != h2) {return h1 - h2;}// 分鐘不相等比較分鐘if (m1 != m2) {return m1 - m2;}// 秒不相等比較秒if (s1 != s2) {return s1 - s2;}// 毫秒不相等比較毫秒if (n1 != n2) {return n1 - n2;}// 都相等按原來順序return 0;}).forEach((a) -> {System.out.println(a);});}更多華為od機試題請點這里<華為od機試題2 真題>,每篇8題,有更多的題也請告訴我啊
總結
以上是生活随笔為你收集整理的华为od机试题1 真题的全部內容,希望文章能夠幫你解決所遇到的問題。