牛客华为机试真题(一)

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);
    }
}

猜你喜欢

转载自blog.csdn.net/a_helloword/article/details/81488131