Bj_实习笔试题汇总

1.带头冲锋(美团)

马拉松项目,超过出发时排在自己前面的任何一个人即可得到表扬,问一共多少人得到表扬?

输入第一行,n,选手数量
输入第二行包括n个正整数,是一个1-n的排列A,表示出发顺序,A[i]表示第i个出发选手的编号。
输入第三行同样包括1-n的排列,表示到达顺序,B[i]表示达到选手的编号。

输入
5
5 3 1 4 2
2 4 5 1 3
输出
3
//思路:用HashSet接收到达选手,每个到达选手分别查看自己身后有没有
//      出发前在自己之前的选手,有的话则+1
import java.util.*;

public class T2 {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        int[] A = new int[n];
        int[] B = new int[n];
        for (int i = 0; i < n; i++) {
            A[i]=sc.nextInt();
        }
        Set behand = new HashSet<>(n);
        for (int i = 0; i < n; i++) {
            B[i]=sc.nextInt();
            behand.add(B[i]);
        }
        int result = 0;

        for (int i = 0; i <n ; i++) {
            int p =B[i];
            behand.remove(p);
            for (int j : A) {
                if(j==p){
                    break;
                }
    //思路:每个到达选手查看自己身后有没有出发前在自己之前的选手
                else if(behand.contains(j)){ 
                    result++;
                    break;
                }

            }
        }
        System.out.println(result);

    }
}

2.数独(华为)

1.判断能否为有效的数独

输入二维数组
[["5","3",".",".","7",".",".",".","."],
 ["6",".",".","1","9","5",".",".","."],
 [".","9","8",".",".",".",".","6","."],
 ["8",".",".",".","6",".",".",".","3"],
 ["4",".",".","8",".","3",".",".","1"],
 ["7",".",".",".","2",".",".",".","6"],
 [".","6",".",".",".",".","2","8","."],
 [".",".",".","4","1","9",".",".","5"],
 [".",".",".",".","8",".",".","7","9"]]
 
class Solution {
    public boolean isValidSudoku(char[][] board) {
        int[][] rows = new int[9][9];
        int[][] col = new int[9][9];
        int[][] sbox = new int[9][9];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j]!='.'){
                    //value=1-->9;index=0-->8,所以num需要减1
                    int num = (int)(board[i][j] - '1');
                    int index_box = (i/3)*3+j/3;
                    if (rows[i][num]==1) { return false;}   
                    //如果该行num出现过一次,即重复出现,不成立,否则num第一次出现,num=1;
                    else { rows[i][num]=1; }
                    if (col[j][num]==1) { return false;}
                    else { col[j][num]=1; }
                    if (sbox[index_box][num]==1)  { return false;}
                    else { sbox[index_box][num]=1; }
                }
            }
        }
        return true;
    }
}

2.解数独

输入
{5,0,0,6,0,2,0,0,0}
{0,0,8,0,7,0,2,0,0}
...................
{9,0,0,0,0,3,0,0,1}

输出
{5,0,0,6,0,2,0,0,0}
{0,0,8,0,7,0,2,0,0}
...................
{9,0,0,0,0,3,0,0,1}
输入
{5,0,0,6,0,2,0,0,0}
{0,0,8,0,7,0,2,0,0}
...................
{9,0,0,0,0,3,0,0,1}

输出
{5,0,0,6,0,2,0,0,0}
{0,0,8,0,7,0,2,0,0}
...................
{9,0,0,0,0,3,0,0,1}

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        int[][] a = new int[9][9];
        Scanner sc = new Scanner(System.in);
        for (int i=0;i<9;i++){
            String s=sc.nextLine();
            s=s.substring(1,s.length()-1);
            String[] c=s.split(",");
            for (int j=0;j<c.length;j++){
                a[i][j]=Integer.parseInt(c[j]);
            }
        }
        sc.close();
        boolean[][] cols = new boolean[9][9];
        boolean[][] rows= new boolean[9][9];
        boolean[][] blocks=new boolean[9][9];
        for (int i =0;i<a.length;i++){
            for (int j=0;j<a.length;j++){
                if(a[i][j]!=0){
                    int k=i/3*3+j/3;
                    int val=a[i][j]-1;
                    rows[i][val]=true;
                    cols[j][val]=true;
                    blocks[k][val]=true;
                }
            }

        }

        DFS(a, cols,rows,blocks);
        for(int i=0;i<9;i++){
            System.out.print("{");
            for (int j=0;j<8;j++){
                System.out.print(a[i][j]+",");
            }
            System.out.println(a[i][8]+"}");
        }
    }

    public static boolean DFS(int[][] a,boolean[][] cols,boolean[][] rows,boolean[][] blocks){
        for (int i=0;i<9;i++){
            for (int j =0;j<9;j++){
                if (a[i][j]==0){
                    int k=i/3*3+j/3;
                    for (int l=0;l<9;l++){
                        if (!cols[j][l]&&!rows[i][l]&&!blocks[k][l]){
                            rows[i][l]=cols[j][l]=blocks[k][l]=true;
                            a[i][j]=1+l;
                            if (DFS(a,cols,rows,blocks)) return true;
                            rows[i][l]=cols[j][l]=blocks[k][l]=false;
                            a[i][j]=0;
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }
}

输入
5 0 0 6 0 2 0 0 0
0 0 8 0 7 0 2 0 0
.................
9 0 0 0 0 3 0 0 1

输出
5 0 0 6 0 2 0 0 0
0 0 8 0 7 0 2 0 0
.................
9 0 0 0 0 3 0 0 1

import java.util.Scanner;

public class T3 {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
            int[][]a=new int[9][9];
            boolean[][] cols = new boolean[9][9];
            boolean[][] rows = new boolean[9][9];
            boolean[][] blocks = new boolean[9][9];//九大宫的九个数字

            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a.length; j++) {
                    a[i][j]=sc.nextInt();
                    if(a[i][j]!=0){
                        int k = i/3*3+ j/3;//划分九宫格,这里以行优先,自己也可以列优先
                        int val=a[i][j]-1;
                        rows[i][val] = true;
                        cols[j][val] = true;
                        blocks[k][val] = true;
                    }
                }
            }//数据装载完毕

            DFS(a, cols, rows, blocks);
            for (int i = 0; i < 9; i++) {
                for (int j = 0; j < 8; j++) {
                    System.out.print(a[i][j]+" ");
                }
                System.out.println(a[i][8]);
            }

        }
    }

    public static boolean DFS(int[][] a,boolean[][] cols,boolean[][] rows,boolean[][] blocks) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if(a[i][j]==0){
                    int k=i/3*3+j/3;
                    for (int l = 0; l < 9; l++) {
                        if(!cols[j][l]&&!rows[i][l]&&!blocks[k][l]){//l对于的数字l+1没有在行列块中出现
                            rows[i][l] = cols[j][l] = blocks[k][l] = true;
                            a[i][j] = 1 + l;//下标加1
                            if(DFS(a, cols, rows, blocks)) return true;//递进则返回true
                            rows[i][l] = cols[j][l] = blocks[k][l] = false;//递进失败则回溯
                            a[i][j] = 0;
                        }
                    }
                    return false;//a[i][j]==0时,l发现都不能填进去
                }//the end of a[i][j]==0
            }
        }
        return true;//没有a[i][j]==0,则返回true
    }
}

3.优惠券(头条)

拥有n种无门槛优惠券,面值为ai,当一件商品售价>=优惠券时即可使用,你要购买m件商品,每件商品售价为bi,问你最少花费多少钱?

输入
3 4
50 100 200
99 199 200 300
输出
248
import java.util.Scanner;

public class toutiao {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();
        int[] A=new int[m];
        int[] B=new int[n];
        for (int i=0;i<m;i++){
            A[i]=sc.nextInt();
        }
        sort(A);   //将优惠券按从小到大排序
        for (int i=0;i<n;i++){
            B[i]=sc.nextInt();
        }

        int res=0;
        int temp=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
        //从小到大搜索最合适的优惠券,差值最小且大于0时最合适
                if (B[i]-A[j]>=0&&B[i]-A[j]<=B[i]-A[0]){  
                    temp=B[i]-A[j];
                }
            }
            res=res+temp;
        }
        System.out.println(res);
    }
    //冒泡排序
    public static int[] sort(int[] arr){
        for(int i  = 0;i<arr.length-1;i++){
             for(int j= 0;j<arr.length-1-i;j++){
                     if(arr[j]>arr[j+1]){
                        int temp = arr[j];
                        arr[j] = arr[j+1];
                         arr[j+1] = temp;
                     }
                 }
             }
        return arr;
     }
}

广联达

1.非空单链表中间节点

public int mmiddleNode(){
	Node cur = this.head;
	int len = getLength()/2;
	for(int i=0;i<len;i++){
		cur = cur.next;
	}
	return cur.val;
}

2.输出N对大括号可能匹配个数

卡特兰数问题
通过剩余左括号和右括号的数来解决:
1.如果剩余左括号的数比右括号数多,则为非法的括号对;
2.如果剩余左括号和右括号数相等,则只能增加一个左括号;
3.如果剩余的左括号数小于右括号数,则可以增加左括号或右括号。

public class test {

    public static void printParenthesis(int pos , int n , int open ,int close ,char[] buffer){

        if(close == n){
            System.out.println(new String(buffer));
            return;
        }
        if(open >close){
            buffer[pos]='}';
            printParenthesis(pos+1, n, open, close+1, buffer);

        }

        if(open <n){
            buffer[pos] = '{';
            printParenthesis(pos+1, n, open+1, close, buffer);
        }

    }
    public static void main(String[] args) {
        int  n = 4;
        char[] cs = new char[8];
        printParenthesis(0, 4, 0, 0, cs);
    }
}

3.股票多少天升值

4.隔离病房

8间隔离病房连着。每天,如果相邻两间病房都被占用或者都空置,则第二天该房间被占用。问第N天情况。

package jianzhioffer;

import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] rooms = new int[8];
        for (int i=0;i<8;i++){
            rooms[i]=sc.nextInt();
        }
        int N=sc.nextInt();
        for (int i=0;i<N;i++){
            rooms=cf(rooms,N);
        }

        for (int i=0;i<8;i++){
            System.out.println(rooms[i]);
        }
    }

    private static int[] cf(int[] rooms,int N) {
        int[] temp=new int[8];
        temp[0]=0;
        temp[7]=0;
        for (int j=1;j<7;j++){
            if (rooms[j-1]==rooms[j+1]){
                temp[j]=1;
            }else temp[j]=0;
        }
        return temp;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值