华为研发工程师编程题--java解法

1.有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        int n;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            n = scanner.nextInt();
            System.out.println(count(n));
        }
    }
 
     static int count(int n){
         if (n <2){
            return 0;
        }else if (n == 2){
            return 1;
        }else{
            int x = n / 3;
            int y = n % 3;
            return x+count(x+y);
        }
    }
}

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

Input Param

n 输入随机数的个数

inputArray n个随机整数组成的数组

Return Value

OutputArray 输出处理后的随机整数

注:测试用例保证输入参数的正确性,答题者无需验证。测试用例不止一组。

import java.util.HashSet;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        HashSet<Integer> temp = new HashSet<Integer>();
 
        while (scanner.hasNext()){
            temp.clear();
            int n = scanner.nextInt();
            for (int i = 0; i < n; i++) {
                int nextInt = scanner.nextInt();
                temp.add(nextInt);
            }
            int index = 0;
            int[] datas = new int[temp.size()];
            for (Integer integer : temp) {
                datas[index++] = integer;
            }
 
            for (int i = 0; i < datas.length - 1; i++) {
                for (int j = i; j < datas.length; j++) {
                    if (datas[i]>datas[j]){
                        int t = datas[i];
                        datas[i] = datas[j];
                        datas[j] = t;
                    }
                }
            }
 
            for (int data : datas) {
                System.out.println(data);
            }
        }
    }
}

3.写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串(多组同时输入 )

import java.math.BigInteger;
import java.util.Scanner;

public class Numeration {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        String regex = "0x[a-fA-F0-9]+";

        while (scanner.hasNext()){
            String source = scanner.next();
            if (source.matches(regex)){
                String substring = source.substring(2).toLowerCase();
                int length = substring.length();
                BigInteger result = new BigInteger("0");
                for (int i = length-1,j = 0; i>=0; i--,j++) {
                    BigInteger multiply = new BigInteger("1");
                    for (int k = 0; k < i; k++) {
                            multiply = multiply.multiply(new BigInteger("16"));
                    }
                    char c = substring.charAt(j);
                    if (c>=48&&c<=57){
                        BigInteger bigInteger = new BigInteger((c-48) + "");
                        BigInteger temp = bigInteger.multiply(multiply);
                        result = result.add(temp);
                    }else if (c>=97&&c<=102){
                        BigInteger bigInteger = new BigInteger( (c-87)+ "");
                        BigInteger temp = bigInteger.multiply(multiply);
                        result = result.add(temp);
                    }
                }
                System.out.println(result);
            }else{
                throw new Exception("请输入正确的16进制数");
            }
        }
    }
}

你可能感兴趣的:(笔试题)