華為機考筆試刷題-java-1



題庫來源

計算字符個數

寫出一個程序,接受一個由字母和數字組成的字符串,和一個字符,然后輸出輸入字符串中含有該字符的個數。不區分大小寫。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            char[] text = sc.nextLine().toLowerCase().toCharArray();
            char targetChar = sc.nextLine().toLowerCase().toCharArray()[0];
            int count=0;
            for (Character t : text) {
                if (t==targetChar) {
                    count=count+1;
                }

            }
            System.out.println(count);

        }
    }

明明的隨機數

他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重復的數字,只保留一個,把其余相同的數去掉,不同的數對應着不同的學生的學號。
再把這些數從小到大排序,按照排好的順序去找同學做調查。

TreeSet與HashSet

HashSet不能保證元素的排列列順序,TreeSet是SortedSet接⼝口的唯一實現類,可以確保集合
元素處於排序狀態
HashSet底層⽤用的是哈希表,TreeSet采⽤用的數據結構是紅黑樹(紅黑樹是一種特定類型的二叉樹)
HashSet中元素可以是null,但只能有一個,TreeSet不不允許放入null
一般使用HashSet,如果需要排序的功能時,才使⽤用TreeSet(性能原因)
import java.util.*;

public class Main
{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){

            TreeSet<Integer> set=new TreeSet<Integer>(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1.compareTo(o2);
                }
            });
            int n=sc.nextInt();
            if(n>0){
                for(int i=0;i<n;i++){
                    set.add(sc.nextInt());
                }
            }
            for(Integer i:set){
                System.out.println(i);
            }
        }
    }
}

字符串數組

•連續輸入字符串,請按長度為8拆分每個字符串后輸出到新的字符串數組;
•長度不是8整數倍的字符串請在后面補數字0,空字符串不處理。

這題的重點在於要將長度不是8的整數倍字符串補齊后循環輸出,而不是輸出同時補齊(太麻煩並且)。
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String line = sc.nextLine();

            int lineLen = line.length();
            if (lineLen % 8 == 0) {
                for (int i = 0; i < lineLen; i = i + 8) {
                    System.out.println(line.substring(i, i + 8));
                }
            } else {
                int lenMod = 8 - lineLen % 8;
                for (int i = 0; i < lenMod; i++) {
                    line = line + 0;
                }
                if (lineLen < 8) {
                    System.out.println(line);
                } else {
                    for (int j = 0; j < lineLen; j = j + 8) {
                        System.out.println(line.substring(j, j + 8));
                    }
                }
            }
        }
    }
}

進制轉換

寫出一個程序,接受一個十六進制的數,輸出該數值的十進制表示。(多組同時輸入 )

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(Long.parseLong(line.substring(2), 16));
        }
    }
}

輸出質因子

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

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);

        long number = 0;

        while(scanner.hasNextLong())
        {
            number = scanner.nextLong();
            isPrimerFactors(number);
        }

    }

    private static void isPrimerFactors(long num)
    {
        long number = num;
        while(number != 1)
        {
            for(int i = 2; i <= number ;i++)
            {
                if(number % i == 0)
                {
                    number /= i;
                    System.out.print(i + " ");
                    break;
                }
            }
        }
    }
}

取近似值

java中的三種取整函數:floor,ceil,round
寫出一個程序,接受一個正浮點數值,輸出該數值的近似整數值。如果小數點后數值大於等於5,向上取整;小於5,則向下取整。

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

}

合並表記錄(TreeMap)

數據表記錄包含表索引和數值(int范圍的整數),請對表索引相同的記錄進行合並,
即將相同索引的數值進行求和運算,輸出按照key值升序進行輸出。
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num=sc.nextInt();
        TreeMap<Integer,Integer> map=new TreeMap<>();
        while(num>0){
            int key=sc.nextInt();
            int value=sc.nextInt();
            if(!map.containsKey(key)){
                map.put(key,value);
            }else{
                map.put(key,map.get(key)+value);
            }
            num--;
        }
        Iterator iterator=map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<Integer,Integer> en = (Map.Entry<Integer,Integer>)iterator.next();
            Integer key=en.getKey();
            Integer value=en.getValue();
            System.out.println(key+" "+value);
        }
    }
}

提取不重復的整數

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

① HashSet的輸出順序是不確定的,但是它的速度最快;

② TreeSet輸出順序是升序排列的,相當於C++中的set,個人比較喜歡這種;

③ LinkedHashSet輸出順序是確定的,就是插入時的順序。
import java.util.*;

/**
 * @Author hwj
 * @Date 2020/8/15 8:37
 * @Desc:
 **/
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] number=sc.nextLine().split("");
        int numLen=number.length;
        LinkedHashSet<Integer> set=new LinkedHashSet<>();
        for(int i=numLen-1;i>=0;i--){
            if(!set.contains(Integer.parseInt(number[i]))) {
                set.add(Integer.parseInt(number[i]));
            }
        }
        Iterator<Integer> iterator= set.iterator();
        while(iterator.hasNext()){
            System.out.print(iterator.next());
        }
    }
}

句子逆序

將一個英文語句以單詞為單位逆序排放。
例如“I am a boy”,逆序排放后為“boy a am I”

   public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String sentence = sc.nextLine();
        String strReverse=reverse(sentence);
        System.out.println(strReverse);
    }
    public static String reverse(String sentence){
        String[] str=sentence.split(" ");
        int strLen=str.length;
        String str2=str[strLen-1];
        for(int i=strLen-2;i>=0;i--){
            str2=str2+" "+str[i];
        }
        return str2;
    }

字串的連接最長路徑查找

給定n個字符串,請對n個字符串按照字典序排列。

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int num=n;
        TreeSet<String> set=new TreeSet<>();
        while(num>=0){
            set.add(sc.nextLine());
            num--;
        }
        Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}

字串的連接最長路徑查找

在線編程適用: BufferedReader 
集合排序 Collections.sort()
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @Author hwj
 * @Date 2020/8/15 8:37
 * @Desc: 
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int num=Integer.parseInt(br.readLine());
        ArrayList<String> arr=new ArrayList<>();
        while(num>0){
            num--;
            arr.add(br.readLine());
        }
        Collections.sort(arr);
        for(String s:arr){
            System.out.println(s);
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM