[杨小白]_牛客_华为研发工程师编程题

301 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看活动详情

前言

小白算法比较菜,希望能激励我每日更新,从leetcode第一题开始,2022年目标300题,记录从0到1的全过程!!

华为研发工程师编程题

华为研发工程师编程题

题目1

某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。

小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。

数据范围:输入的正整数满足 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void  main(String[] args) throws IOException{
        //Scanner sanner = new Scanner(System.in);
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Integer> input = new ArrayList<>();
        int t = Integer.parseInt(bf.readLine());
        while(t != 0){
            input.add(t);
            t = Integer.parseInt(bf.readLine());
        }
        for(int i = 0; i < input.size(); i++) {
            fun(input.get(i));
        }
        return;
    }
    public static void fun(int n) {
        int res = 0;
        while(n >= 3) {
            n = n - 2;
            res++;
        }
        if(n==2) {
            System.out.println(++res);
        } else {
            System.out.println(res);
        }
    }
}

提交排名

100开外,内存大小9488KB,时间14ms

解析

这个题并没有什么难度,直接讲n每次减2,知道n小于3,此时判断n是否等于2,如果等于2,结果再加一即可,如果不等于2,就直接返回结果。

题目2

明明生成了N个1到500之间的随机整数。请你删去其中重复的数字,即相同的数字只保留一个,把其余相同的数去掉,然后再把这些数从小到大排序,按照排好的顺序输出。

数据范围:  ,输入的数字大小满足 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] input = new int[501];
        for(int i = 0; i < n; i++){
            int t = Integer.parseInt(br.readLine());
            input[t]++;
        }
        for(int i = 0; i < 501; i++) {
            if(input[i]!=0) {
                System.out.println(i);
            }
        }
    }
}

排名也在100开外,耗时20ms,内存9656km,时间复杂度和空间复杂度都是O(500) 肯定比O(n)要大,思路简单。也不需要排序了。

题目3

写出一个程序,接受一个十六进制的数,输出该数值的十进制表示。

数据范围:保证结果在 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.Math;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int res = 0;
        char[] chars = str.toCharArray();
        int t = 0;
        for(int i = chars.length - 1; i > 1 ; i--,t++) {
            if(chars[i]=='A') {
                res = res + 10 * (int)Math.pow(16,t);
            } else if (chars[i]=='B') {
                res = res + 11 * (int)Math.pow(16,t);
            } else if (chars[i]=='C') {
                res = res + 12 * (int)Math.pow(16,t);
            } else if (chars[i]=='D') {
                res = res + 13 * (int)Math.pow(16,t);
            } else if (chars[i]=='E') {
                res = res + 14 * (int)Math.pow(16,t);
            } else if (chars[i]=='F') {
                res = res + 15 * (int)Math.pow(16,t);
            } else {
                res = res + (chars[i] - '0') * (int)Math.pow(16,t);
            }
        }
        System.out.println(res);
    }
}

考虑到输入的是字符串,直接拿走最前面的0x然后从最低位开始累乘累加即可。 耗时14ms 内存9480KB

3.结束

模拟机考,一个小时内三道题都能ac,但是没考虑到任何的优化,导致每个的时间,空间复杂度都不低,最后的排名也不算好。但是机考时,能ac 就是王道,别想着什么优化了,只有在面试的时候,提出自己的优化方案和见解才能让面试官眼前一亮。