1. 程式人生 > >牛客華為機試真題(一)

牛客華為機試真題(一)

1.字串最後一個單詞的長度

題目描述
計算字串最後一個單詞的長度,單詞以空格隔開。

輸入描述
一行字串,非空,長度小於5000。

輸出描述
整數N,最後一個單詞的長度。

輸入示例
hello world

輸出示例
5

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String str = read.nextLine();
        str = str.substring(str.lastIndexOf(" "
) + 1, str.length()); System.out.println(str.length()); } }

next()方法和nextLine()方法的區別,next() 方法遇見第一個有效字元(非空格,非換行符)時,開始掃描,當遇見第一個分隔符或結束符(空格或換行符)時,結束掃描,獲取掃描到的內容,即獲得第一個掃描到的不含空格、換行符的單個字串。使用nextLine()時,則可以掃描到一行內容並作為一個字串而被獲取到。

2.計算字元個數

題目描述
寫出一個程式,接受一個有字母和數字以及空格組成的字串,和一個字元,然後輸出輸入字串中含有該字元的個數。不區分大小寫。

輸入描述
輸入一個有字母和數字以及空格組成的字串,和一個字元。

輸出描述
輸出輸入字串中含有該字元的個數。

輸入示例
ABCDEF A

輸出示例
1

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String str = read.next().toUpperCase();
        char c = read.next().toUpperCase().charAt(0
); int sum = 0; for(int i = 0; i < str.length(); i++) { if(c == str.charAt(i)) { sum++; } } System.out.println(sum); } }

3.明明的隨機數

題目描述
明明想在學校中請一些同學一起做一項問卷調查,為了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應著不同的學生的學號。然後再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作(同一個測試用例裡可能會有多組資料,希望大家能正確處理)。

輸入描述
輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數

輸出描述
返回多行,處理後的結果

輸入示例
11
10
20
40
32
67
40
20
89
300
400
15

輸出示例
11
10
20
40
32
67
40
20
89
300
400
15

程式碼1

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            int num = read.nextInt();
            Set<Integer> result = new TreeSet<>();
            for(int i = 0; i < num; i++) {
                result.add(read.nextInt());
            }
            Iterator<Integer> iterator = result.iterator();
            while(iterator.hasNext()) {
                System.out.println(iterator.next());
            }
        }
    }
}

程式碼2

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            int num = read.nextInt();
            int[] result = new int[num];
            for(int i = 0; i < num; i++) {
                result[i] = read.nextInt();
            }
            Arrays.sort(result);
            System.out.println(result[0]);
            for(int i = 1; i < result.length; i++) {
                if(result[i] == result[i - 1]) {
                    continue;
                } else {
                    System.out.println(result[i]);
                }
            }
        }
    }
}

4.字串分隔

題目描述
•連續輸入字串,請按長度為8拆分每個字串後輸出到新的字串陣列;
•長度不是8整數倍的字串請在後面補數字0,空字串不處理。

輸入描述
連續輸入字串(輸入2次,每個字串長度小於100)

輸出描述
輸出到長度為8的新字串陣列

輸入示例
abc
123456789

輸出示例
abc00000
12345678
90000000

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            String str = read.nextLine();
            while(str.length() >= 8) {
                System.out.println(str.substring(0, 8));
                str = str.substring(8, str.length());
            }
            if(str.length() > 0 && str.length() < 8) {
                str = str + "0000000";
                System.out.println(str.substring(0, 8));
            }
        }
    }
}

5.進位制轉換

題目描述
寫出一個程式,接受一個十六進位制的數值字串,輸出該數值的十進位制字串。(多組同時輸入 )

輸入描述
輸入一個十六進位制的數值字串。

輸出描述
輸出該數值的十進位制字串。

輸入示例
0xA

輸出示例
10

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            String str = read.next().substring(2);
            System.out.println(Integer.parseInt(str, 16));
        }
    }
}

6.質數因子

題目描述
功能:輸入一個正整數,按照從小到大的順序輸出它的所有質數的因子(如180的質數因子為2 2 3 3 5 )
最後一個數後面也要有空格

輸入描述
輸入一個long型整數

輸出描述
按照從小到大的順序輸出它的所有質數的因子,以空格隔開。最後一個數後面也要有空格。

輸入示例
180

輸出示例
2 2 3 3 5

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            long num = read.nextLong();
            while(num != 1) {
                for(int i = 2; i <= num; i++) {
                    if(num % i == 0) {
                        System.out.print(i + " ");
                        num = num / i;
                        break;
                    }
                }
            }
        }
    }
}

7.取近似值

題目描述
寫出一個程式,接受一個正浮點數值,輸出該數值的近似整數值。如果小數點後數值大於等於5,向上取整;小於5,則向下取整。

輸入描述
輸入一個正浮點數值

輸出描述
輸出該數值的近似整數值

輸入示例
5.5

輸出示例
6

程式碼1

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            float num = read.nextFloat();
            float temp = num;
            temp = temp - (int)temp;
            if(temp >= 0.5) {
                System.out.println((int)num + 1);
            } else {
                System.out.println((int)num);
            }
        }
    }
}

程式碼2

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        while(read.hasNext()) {
            float num = read.nextFloat();
            System.out.println(Math.round(num));
        }
    }
}

8.合併表記錄

題目描述
資料表記錄包含表索引和數值,請對錶索引相同的記錄進行合併,即將相同索引的數值進行求和運算,輸出按照key值升序進行輸出。

輸入描述
先輸入鍵值對的個數
然後輸入成對的index和value值,以空格隔開

輸出描述
輸出合併後的鍵值對(多行)

輸入示例
4
0 1
0 2
1 2
3 4

輸出示例
0 3
1 2
3 4

程式碼

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        int num = read.nextInt();
        Map<Integer, Integer> map = new TreeMap<>();
        for(int i = 0; i < num; i++) {
            int key = read.nextInt();
            int value = read.nextInt();
            if(map.containsKey(key)) {
                map.put(key, map.get(key) + value);
            } else {
                map.put(key, value);
            }
        }
        for(Map.Entry<Integer, Integer> entry:map.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}

9.提取不重複的整數

題目描述
輸入一個int型整數,按照從右向左的閱讀順序,返回一個不含重複數字的新的整數。

輸入描述
輸入一個int型整數

輸出描述
按照從右向左的閱讀順序,返回一個不含重複數字的新的整數

輸入示例
9876673

輸出示例
37689

程式碼

import java.util.*;
public  class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        int num = read.nextInt();
        List<Integer> list = new ArrayList<>();
        while(num > 0) {
            if(list.contains(num % 10)) {
                num = num / 10;
                continue;
            } else {
                 list.add(num % 10);
                 num = num / 10;
            }
        }
        for(Integer i:list) {
            System.out.print(i);
        }
    }
}

10.字元個數統計

編寫一個函式,計算字串中含有的不同字元的個數。字元在ACSII碼範圍內(0~127)。不在範圍內的不作統計。

輸入描述
輸入N個字元,字元在ACSII碼範圍內。

輸出描述
輸出範圍在(0~127)字元的個數。

輸入示例
abc

輸出示例
3

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        String input = read.next();
        char[] arr = input.toCharArray();
        Map<Character, Integer> result = new HashMap<>();
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            if(!result.containsKey(arr[i])) {
                result.put(arr[i], 1);
                sum++;
            }
        }
        System.out.println(sum);
    }
}