秋招笔试记录

荣耀笔试题2021-8-7晚

题目描述:
小华刚刚参加了一个编译器课程,他想设计实现自己的编译器。首先,他设计了一种语言,他的语言最大支持N个字不同的字符,并且他规定了由这些字符组成的ID,任何ID的长度需要大于等于1且小于等于L个字符,他希望设计一个程序知道他的语言总共能组成多少个ID。例如,当N=2(假设字符可以是0或1),并且L=3时,他具有如下的ID:{0,1,00,01,10,11,000,001,010,011,100,101,110,111},因此当N=2,L=3时总共有14中ID。
你需要编写一个程序,可以帮助小华找到可能的ID的总数。由于答案可能非常大,最后的结果需要对1000000007取余。
输入描述:
输入包含多个用例。每个用例将为包含两个整数N和L的一行。N是可以作为id的一部分字符数,L是该语言支持的最大长度(1 <= N <= 65535,1 0 <= L <= 10^5).
当N=0并且L等于0时表示输入结束。
示例1:
输入

2 3
100 15
0 0

输出

14
979451521

实现的代码:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main1 {
    public static void main(String[] args) {
        //ArrayList<Integer> save = new ArrayList<Integer>(); //不能声明在这里
        ArrayList<ArrayList<Integer>> save_all = new ArrayList<ArrayList<Integer>>();
        Scanner scanner = new Scanner(System.in);
        while(true){
            int N = scanner.nextInt();
            int L = scanner.nextInt(); //N 和 L为一行输入的值,直接读取一行输入的两个数,并且得到整型数
            if(N == 0 && L == 0){
                break;
            }
            ArrayList<Integer> save = new ArrayList<Integer>(); //必须声明在这里,对每次的输入新建一个对象,不然定义在循环外面,每行的值都会累加到一起
            save.add(N);save.add(L);
            save_all.add(save);
            //save.clear();
        }
        int size = save_all.size(); //输入多少行
        long result = 0;
        //System.out.println("行数:" + size + " " + "值为:" + save_all.toString()); //注意list的输出
        for (int i = 0; i < size; i++) {
            ArrayList<Integer> arrayList = save_all.get(i);
           // System.out.println(arrayList.toString());
            Integer[] line = arrayList.toArray(new Integer[2]); //注意这种写法,将arrayList转换成整型数组
            result = fun(line[0], line[1]) % 1000000007;
            System.out.println(result);
//            for(Integer num : line){
//                System.out.println(num);
//            }
            //System.out.println("==============");
        }
    }
    public static long fun(int n, int l){
        long res = 0;
        for (int i = 1; i <= l ; i++) {
            res += math(n, i);
        }
        return res;
    }
    public static long math(int x, int y){
        long res = 1;
        for (int i = 0; i < y; i++) {
            res *= x;
            res = res % 1000000007;
        }
        return res;
    }
}

测试输入输出

import java.util.ArrayList;
import java.util.Scanner;

public class Main1 {
    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> save_all = new ArrayList<ArrayList<Integer>>();
        Scanner scanner = new Scanner(System.in);
        while(true){
            int N = scanner.nextInt();
            int L = scanner.nextInt();
            if(N == 0 && L == 0){
                break;
            }
            ArrayList<Integer> save = new ArrayList<Integer>(); //必须声明在这里,对每次的输入新建一个对象,不然定义在循环外面,每行的值都会累加到一起
            save.add(N);save.add(L);
            save_all.add(save);
        }
        int size = save_all.size(); //输入多少行
        System.out.println("行数:" + size + " " + "值为:" + save_all.toString()); //注意list的输出
        for (int i = 0; i < size; i++) {
            ArrayList<Integer> arrayList = save_all.get(i);
            System.out.println(arrayList.toString());
        }
    }
}

测试:
在这里插入图片描述
第二种写法

import java.util.ArrayList;
import java.util.Scanner;
public class Main1 {
    public static void main(String[] args) {
        ArrayList<int[]> save_all = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while(true){
            int N = scanner.nextInt();
            int L = scanner.nextInt();
            if(N == 0 && L == 0){
                break;
            }
            int[] save = new int[2];
            save[0] = N; save[1] = L;
            save_all.add(save);
        }
        int size = save_all.size(); //输入多少行
        System.out.println("行数:" + size + " " + "值为:" + save_all.toString()); //注意list的输出
        for (int i = 0; i < size; i++) {
            int[] ints = save_all.get(i);
            System.out.println(ints[0]+" "+ints[1]);
        }
    }
}

在这里插入图片描述

第三题 成绩排序
10名学生,基础学科语文、数学、英语
录取条件:(1)基础学科总分100分,及格成绩60分
(2)没有不及格科目
(3)录取排名前3的考生
约束限制:
(1)优先考察基础学科总分排名
(2)基础学科总分相同是,排名再一次分别查看单科语文、数学、英语三门单科成绩
(3)如果成绩完全相同,排名也相同。同一个排名的可能存在多位考生
(4)排名相同是,按姓名(汉语拼音)的ASCII码从小到大排序
(5)排序后,分别输出考试合格考生的名单和被录取的名单
示例1 输入:

goudan2 60 80 80
zhaowu 60 80 80
zhangsan 60 80 80
yatou 90 80 80
goudan1 60 80 80
gousheng 80 100 60
lilei 80 100 60
hanmingmei 80 90 60
liutao 80 100 60
SimonYin 80 100 60

输出

[First round name list]
yatou 90 80 80
SimonYin 80 100 60
gousheng 80 100 60
lilei 80 100 60
liutao 80 100 60
hanmingmei 80 90 60
goudan1 60 80 80
goudan2 60 80 80
zhangsan 60 80 80
zhaowu 60 80 80

[Final name list]
yatou 90 80 80
SimonYin 80 100 60
gousheng 80 100 60
lilei 80 100 60
liutao 80 100 60
hanmingmei 80 90 60

对应的代码

import java.util.*;
public class Mian3_1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int studentNum = 10;
        //int[][] arr = new int[studentNum][5];
        List<Student> studentList= new ArrayList<Student>();

        for(int i = 0; i < studentNum; i++){
            String s = scanner.nextLine();
            String[] strArr  = s.split(" ");
            String name;
            int chi;
            int mat;
            int eng;
            name = strArr[0];
            chi = Integer.valueOf(strArr[1]);
            mat = Integer.valueOf(strArr[2]);
            eng = Integer.valueOf(strArr[3]);

            int sum = 0;
            if(chi >= 60 && mat >= 60 && eng >= 60){
                sum = chi + mat + eng;
            }
            studentList.add(new Student(name, chi, mat, eng, sum));
        }
        Collections.sort(studentList);

        System.out.println("[First round name list]");
        for(int i = 0; i < studentNum; i++){
            if(studentList.get(i).sum == 0){
                continue;
            }
            System.out.println(studentList.get(i).name+" "+studentList.get(i).Chinese+" "+
                    +studentList.get(i).Math+" "+studentList.get(i).English);
        }
        System.out.println("");
        System.out.println("[Final name list]");
        //获取分数前三的所有分,包括重分的
//        int index = 0;
//        int resNum = 0;
//        for(int i=0; i<studentNum-1; i++){
//           // System.out.println(i);
//            if(studentList.get(i).sum != studentList.get(i+1).sum){
//                index++;
//               // System.out.println(studentList.get(i).sum+"!="+studentList.get(i+1).sum+"change:"+index);
//            }
//            if(index == 3){
//                resNum = i;
//               // System.out.println("结束索引:"+resNum);
//                break;
//            }
//        }
//        for(int i=0; i<=resNum; i++){
//            System.out.println(studentList.get(i).name + " " + studentList.get(i).Chinese + " "
//                    + studentList.get(i).Math + " " + studentList.get(i).English );
//        }

        int inum = 0;

        for(int i = 0; i < 3; i++){
            if(inum >= studentNum){
                break;
            }
            System.out.println(studentList.get(inum).name+" "+studentList.get(inum).Chinese+" "+
                    +studentList.get(inum).Math+" "+studentList.get(inum).English);
            if(studentList.get(inum+1).sum == studentList.get(inum).sum
                    && studentList.get(inum+1).Chinese == studentList.get(inum).Chinese
                    && studentList.get(inum+1).Math == studentList.get(inum).Math
                    && studentList.get(inum+1).English == studentList.get(inum).English){
                i--;
            }
            inum++;
        }
    }
    public static class Student implements Comparable<Student>{
        String name;
        int Chinese;
        int Math;
        int English;
        int sum;
        public Student(String n, int c, int m, int e, int s){
            this.name = n;
            this.Chinese = c;
            this.Math = m;
            this.English = e;
            this.sum = s;
        }
        @Override
        public int compareTo(Student s) {
            if(this.sum != s.sum){
                return s.sum - this.sum;
            }
            if(this.Chinese != s.Chinese){
                return s.Chinese - this.Chinese;
            }
            if(this.Math != s.Math){
                return s.Math - this.Math;
            }
            if(this.English != s.English){
                return s.English - this.English;
            }
            if(this.name != s.name){
                return this.name.compareTo(s.name);  //字符串(姓名)排名默认排名 升序
                //return s.name.compareTo(this.name);  //降序
            }
            return 0;
        }
    }
}

贝壳笔试题 8月13晚

牛妹拿到了一个只由小写字母组成的字符串s,接下来将字符串执行k次操作,每次操作都会把s中ASCII码最小的字母从s中删除,请返回k次操作之后的字符串s。
示例1:
输入:

"caabeefa",2

输出:

"ceef"

代码

    public static String NS(String s, int k){
        ArrayList<Character> characters = new ArrayList<>();
        for(int m = 0; m < s.length(); m++){
            if(!characters.contains(s.charAt(m))){
                characters.add(s.charAt(m));
           }
        }
        Collections.sort(characters);
        String result = s;
        for(int n = 0; n < k; n++){
            result = result.replace(characters.get(n).toString(),"");
        }
        return result;
    }

美团笔试 8月15日上午

机器人相撞题的前序
leetcode20有效的括号
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:
1.左括号必须用相同类型的右括号闭合。
2.左括号必须以正确的顺序闭合。
示例1:

输入:s = "()"
输出:true

示例2:

输入:s = "()[]{}"
输出:true

示例3

输入:s = "(]"
输出:false

示例4

输入:s = "([)]"
输出:false

示例5

输入:s = "{[]}"
输出:true
import java.util.*;
//力扣题,
public class LeetCode {
    public static boolean isValid2(String s){
        if(s.length()%2 == 0){
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if(c == '(' || c == '[' || c == '{'){
                    stack.push(c);
                }else if(c == ')'){
                    if(!stack.isEmpty() && stack.peek() == '('){
                        stack.pop();
                    }else{
                        return false;
                    }
                }else if(c == '}'){
                    if(!stack.isEmpty() && stack.peek() == '{'){
                        stack.pop();
                    }else{
                        return false;
                    }
                }else{
                    if(!stack.isEmpty() && stack.peek() == '['){
                        stack.pop();
                    }else{
                        return false;
                    }
                }
            }
            return stack.isEmpty();
        }
        return false;
    }
    public static boolean isValid(String s){
        int length = s.length();
        if(length % 2 == 1){
            return false;
        }
        Map<Character, Character> pairs = new HashMap<Character, Character>(){{
            put(')', '(');
            put(']', '[');
            put('}', '{'); //将右边对应括号设置为key,左边对应的括号设置为true
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < length; i++) {
            char ch = s.charAt(i);
            if(pairs.containsKey(ch)){
                if(stack.isEmpty() || stack.peek() != pairs.get(ch)){
                    return false;
                }
                stack.pop(); //stack.pop()与stack.peek()都返回栈顶的值,peek不删除栈顶的值,pop删除栈顶的值
            }else{
                stack.push(ch); //肯定先会存放左边的括号
            }
        }
        return stack.isEmpty();
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String s = scanner.nextLine();
            System.out.println(isValid2(s));
        }
    }
}

小美的机器人
数轴上有若干机器人,移动速度均为1,如果机器人重合就会发生爆炸,爆炸后消失。举例:如果三个机器人,一个位于0,向右;一个位于2,向右;一个位于4,向左,则时刻1时候,后面两个机器人在位置3相遇并发生爆炸。只有整数时刻机器人才会检查重合,如果两个机器人,一个位于位置1,向右,一个位于位置2,向左,则它们并不会在整数时刻重合,因袭它们两个不存在相遇爆炸。机器人初始时刻不会重叠。
输入描述:

第一行 正整数n,代表有n个机器人
接下来n行,分别为代表机器人坐标(正整数)与方向(LR)

输出描述:

输出n行,表示该机器人爆炸时间,若该机器人不会爆炸,输出-1

示例输入:

10
94 R
74 L
90 L
75 R
37 R
99 R
62 R
4 L
92 L
44 R

示例输出:

-1
6
23
-1
-1
-1
6
-1
-1
23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值