zoukankan      html  css  js  c++  java
  • 华为机考笔试刷题-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);
            }
        }
    }
    
  • 相关阅读:
    SPOJ SAMER08A
    SPOJ TRAFFICN
    CS Academy Set Subtraction
    CS Academy Bad Triplet
    CF Round 432 C. Five Dimensional Points
    CF Round 432 B. Arpa and an exam about geometry
    SPOJ INVCNT
    CS Academy Palindromic Tree
    身体训练
    简单瞎搞题
  • 原文地址:https://www.cnblogs.com/alidata/p/13507291.html
Copyright © 2011-2022 走看看