【Java】 牛客网华为机试108题汇总

1、求字符串最后一个单词长度

  • 计算字符串最后一个单词的长度,单词以空格隔开。
import java.util.Scanner;

/**
 * @Author: Stephen
 * @Date: 2020/3/21 13:24
 * @Content: 计算字符串最后一个单词的长度,单词以空格隔开。
 */
public class StrLength01 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()){
            String[] str = input.nextLine().split(" ",-1);
            System.out.println(str[str.length-1].length());
        }
    }
}

2、计算字符串个数

  • 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,
  • 然后输出输入字符串中含有该字符的个数。不区分大小写。
import java.util.Scanner;

/**
 * @Author: Stephen
 * @Date: 2020/3/21 14:17
 * @Content: 写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,
 * 然后输出输入字符串中含有该字符的个数。不区分大小写。
 */
public class WordCount {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String cha = input.nextLine();
        int count = 0;
        if (str != null && str.length()>0){
            for (int i=0;i<str.length();i++){
                if (cha.toLowerCase().charAt(0)==str.toLowerCase().charAt(i)){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

3、明明的随机数

  • 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),
  • 对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。
  • 然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。
  • 请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
import java.util.*;

/**
 * @Author: Stephen
 * @Date: 2020/3/21 14:45
 * @Content:
 * 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤1000),
 * 对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。
 * 然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。
 * 请你协助明明完成“去重”与“排序”的工作(同一个测试用例里可能会有多组数据,希望大家能正确处理)。
 */
public class RandomTest {
    public static void main(String[] args) {
         Scanner num = new Scanner(System.in);
        while (num.hasNext()){
            int number = num.nextInt();
            Set<Integer> numbers = new HashSet<Integer>();
            List<Integer> list = new ArrayList<Integer>();
            for (int i =0;i<number;i++){
                // 曲解了题意误以为随机输入是用随机数输入
//            Random rand = new Random();
//            int a=rand.nextInt(1001);
                numbers.add(num.nextInt());
            }
            for (int a:numbers){
                list.add(a);
            }
            Collections.sort(list);
            for (int a:list){
                Systems.out,println(a);
            }
        }
    }
}

写的时候是用hashset去重再转arrayList排序,经阅读代码补充使用treeset直接去重排序treeset详解

import java.util.*;
public class RandomTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){

            int num = sc.nextInt();
            TreeSet<Integer> set = new TreeSet<Integer>();
            for(int i = 0 ; i < num ;i++){
                int curr = sc.nextInt();
                set.add(curr);
            }
            for(Integer i : set){
                System.out.println(i);
            }
        }
    }
}

4、字符串分割

  • 连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
  • 长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
import java.util.Scanner;

/**
 * @Author: Stephen
 * @Date: 2020/3/21 15:38
 * @Content:
 * •连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
 * •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
 */
public class StrSplit {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()){
            String str = input.nextLine();
            while (str.length()>=8){
                System.out.println(str.substring(0,8));
                str=str.substring(8);
            }
            if (str.length()<8 && str.length()>0){
                str = str+"0000000";
                System.out.println(str.substring(0,8));
            }
        }
    }
}

5、进制转换

  • 写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
import java.util.Scanner;

/**
 * @Author: Stephen
 * @Date: 2020/3/21 15:57
 * @Content:
 * 写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。(多组同时输入 )
 */
public class SystemTransform {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNext()){
            String str = sc.nextLine();
            System.out.println(fun(str.substring(2)));
        }
    }

    public static int fun(String s){
        int n=0;
        int count= 0;
        int temp = 0;
        char ch;

        while(count<s.length())
        {
            ch = s.charAt(s.length()-count-1);
            if(ch>='0'&&ch<='9'){
                temp = ch-'0';
            }else if(ch>='A'&&ch<='Z'){
                temp = ch-'A'+10;
            }else if(ch>='a'&&ch<='z'){
                temp = ch-'a'+10;
            }else{
                break;
            }
            n += temp*Math.pow(16,count);
            count++;
        }

        return n;
    }
}

6、质数因子

  • 输入一个正整数,按照从小到大的顺序输出它的所有质因子(如180的质因子为2 2 3 3 5 )
  • 最后一个数后面也要有空格
package com.njbdqn.services;

import java.util.Scanner;

/**
 * @Author: Stephen
 * @Date: 2020/3/23 21:46
 * @Content:
 * 输入一个正整数,按照从小到大的顺序输出它的所有质因子(如180的质因子为2 2 3 3 5 )
 * 最后一个数后面也要有空格
 */
public class Primefactors {
    public static void main(String[] args) {
        public static void main(String [] args)
        {
            Scanner sc=new Scanner(System.in);
            long params=sc.nextLong();
            if(params<2)
            {
                sc.close();
                return ;
            }
            String result =getResult(params);
            System.out.println(result);
            sc.close();

        }
    }
    public static String getResult(long ulDataInput){
        StringBuilder str=new StringBuilder();
        int index=2;
        while(index<=ulDataInput)
        {
            if(ulDataInput%index==0){
                if(index==ulDataInput){
                    str.append(index).append(" ");
                    break;
                }else{
                    str.append(index).append(" ");
                    ulDataInput=ulDataInput/index;
                }
            }else
            {
                index++;
            }
        }
        return str.toString();
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章