华为研发工程师编程题--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进制数");
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值