华为研发工程师编程题 Java实现


汽水瓶

在这里插入图片描述
我的代码

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n;
        int sum;
        while (in.hasNext()) {
            sum = 0;
            n = in.nextInt();
            if (n == 0) {
                return;
            }
            while (n >= 3) {
                sum += (n / 3);
                n = (n / 3) + (n - (n / 3) * 3);
            }
            if (n == 2) {
                sum++;
            }
            System.out.println(sum);
        }
    }
}

其它思路:通过分析,每两个瓶子就可以换一瓶汽水,其中向老板借一个,凑成三个瓶子,能兑换一瓶,喝完以后,变成空瓶子还给老板

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n;

        while (in.hasNextInt()) {
            n = in.nextInt();
            if (n == 0) {
                return;
            }
            System.out.println(n / 2);
        }
    }
}

明明的随机数

在这里插入图片描述

我的代码:使用TreeSet

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int num;
        Set<Integer> set = new TreeSet<>();
        while (N > 0) {
            N--;
            num = in.nextInt();
            set.add(num);
        }
        for (int i : set) {
            System.out.println(i);
        }
    }
}

其它思路:根据题目所给的取值范围,开辟一个数组

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int num;
        int[] arr = new int[1001];
        while (N > 0) {
            N--;
            num = in.nextInt();
            arr[num]++;
        }
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > 0) {
                System.out.println(i);
            }
        }
    }
}

进制转换

在这里插入图片描述

我的代码

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int num = 0;
        for (int i = 2; i < str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
                case 'A':
                case 'a':
                    num = num * 16 + 10;
                    break;
                case 'B':
                case 'b':
                    num = num * 16 + 11;
                    break;
                case 'C':
                case 'c':
                    num = num * 16 + 12;
                    break;
                case 'D':
                case 'd':
                    num = num * 16 + 13;
                    break;
                case 'E':
                case 'e':
                    num = num * 16 + 14;
                    break;
                case 'F':
                case 'f':
                    num = num * 16 + 15;
                    break;
                default:
                    num = num * 16 + (c - '0');
                    break;
            }
        }
        System.out.println(num);
    }
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int num = 0;
        for (int i = 2; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 'A' && c <= 'F') {
                num = num * 16 + c - 'A' + 10;
            } else if (c >= 'a' && c <= 'f') {
                num = num * 16 + c - 'a' + 10;
            } else {
                num = num * 16 + c - '0';
            }
        }
        System.out.println(num);
    }
}

其它思路:

Integer.parseInt(s, radix)可以转化,其中s为输入字符串,radix为进制数,返回值为int型十进制整数。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        System.out.println(Integer.parseInt(str.substring(2),16));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值