一些往年校招题目

在学校的日子也只剩下一个多月,这几天一直在和朋友聚会,玩耍,差点忘记了今天有博客需要写。上次我说,找十道校招编程题,这两个星期做了有十几道,有一些没有记录一下。也参与好几个校招的笔试,Ac了一些,题目也没有记下来。
所以,只能选取五道我还记得并且有一些些难度的题目。

1、格子距离

地址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3359&konwledgeId=40

import java.util.Scanner;

public class Main {

    static int[][] b = null;
    static int[][] a = null;
    static int n;
    static int m;
    static int startI;
    static int startJ;
    static int[][] position = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int k = s.nextInt();
        for (int c = 0; c < k; c++) {
            n = s.nextInt();
            m = s.nextInt();
            startI = s.nextInt() - 1;
            startJ = s.nextInt() - 1;
            a = new int[n][m];
            // 储存墙
            b = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    b[i][j] = s.nextInt();
                    a[i][j] = -1;
                }
            }
            a[startI][startJ] = 0;
            walk(startI, startJ, -1);
            print(a, c + 1);
        }
    }

    static void walk(int x, int y, int dir) {
        if (x < 0 || x >= n || y < 0 || y >= m) {
            return;
        }
        for (int i = 0; i < 4; i++) {
            int x1 = position[i][0];
            int y1 = position[i][1];
            int tempX = x + x1;
            int tempY = y + y1;
            int position = getPosition(x, y, tempX, tempY);
            if (judge(position, x, y, tempX, tempY, dir)) {
                int temp = a[x][y] + 1;
                if (a[tempX][tempY] == -1 || temp < a[tempX][tempY]) {
                    a[tempX][tempY] = temp;
                    walk(tempX, tempY, position);
                }
            }
        }
    }

    private static boolean judge(int psoition, int x, int y, int tempX, int tempY, int dir) {
        if (tempX == startI && tempY == startJ) {
            return false;
        }
        if (tempX < 0 || tempX >= n || tempY < 0 || tempY >= m) {
            return false;
        }
        String wall = "0000" + Integer.toBinaryString(b[x][y]);
        int length = wall.length();
        boolean isHasWalk = false;
        switch (psoition) {
        case 1:// 左边
            if (dir == 2) {
                return false;
            }
            isHasWalk = !((b[x][y]&4)==0);
            break;
        case 2:// 右边
            if (dir == 1) {
                return false;
            }
            isHasWalk = !((b[x][y]&3)==0);
            break;
        case 3:// 上边
            if (dir == 4) {
                return false;
            }
            isHasWalk = !((b[x][y]&1)==0);

            break;
        case 4:// 下边
            if (dir == 3) {
                return false;
            }
            isHasWalk = !((b[x][y]&2)==0);

            break;
        default:
            return false;
        }
        return !isHasWalk;
    }

    private static int getPosition(int x, int y, int tempX, int tempY) {
        if (x - tempX > 0) {// 向上走
            return 3;
        }
        if (x - tempX < 0) {// 向下走
            return 4;
        }
        if (y - tempY > 0) {// 向左走
            return 1;
        }
        if (y - tempY < 0) {// 向右走
            return 2;
        }
        return -1;
    }

    static void print(int[][] a, int b) {
        System.out.println("Case " + b + ":");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

这道题没有Ac,但是思路是对了。当时做完之后,刚好下课。回来就把一道题给忘了。后面找时间完善一下。

2、十字架

题目地址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3373&konwledgeId=40

import java.util.Scanner;

public class Main1 {

    static String[][] a = null;
    static int n = 0;
    static int x = 0;
    static int height = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        for (int b = 1; b <= k; b++) {//组数
            n = scanner.nextInt()-1;//减一是因为计算中心点的距离时,比输入的n大了1,统一在这里减去1
            if(n < 0){//安全防备
                System.out.println("o");
                continue;
            }
            //计算矩阵的长宽,因为是正方形,这里只计算高度
            height = (int) Math.pow(3, n);
            //创造矩阵
            a = new String[height][height];
            //初始化矩阵
            for (int i = 0; i < a.length; i++) {
                for (int j = 0; j < a[0].length; j++) {
                    a[i][j] = " ";
                }
            }
            //计算最中心的点
            x = (height - 1) / 2;
            int y = x;
            //开始绘制
            drwas(x, y,height,0,height,0, n);
            print(a,b);
        }
    }

    private static void drwas(int x1, int y1, int Yhight, int Ylow, int Xhight, int Xlow,int t) {
        //递归出口,如果要绘制的点超出边界,就放回
        //或者t已经计算了n次,此时已经覆蓋到当前中心点的所有子中心点了
        if ( x1 < Xlow || y1 < Ylow || x1 > Xhight || y1 > Yhight || t < 0) {
            return;
        }
        // 绘制最小中心点
        a[x1][y1] = "o";
        // 这是中心点的距离
        int distance = (int) ((int) (Math.pow(3, t) - 1) / 2 - (Math.pow(3, t - 1) - 1) / 2);
        //下面四个,分别是绘制上下左右的中心点
        drwas(x1 - distance,y1,height,0,height,0, t - 1);
        drwas(x1 + distance, y1,height,0,height,0, t - 1);
        drwas(x1, y1 - distance,height,0,height,0, t - 1);
        drwas(x1, y1 + distance,height,0,height,0,  t - 1);
        //绘制本身的中心点
        drwas(x1, y1, y1 + distance / 2, y1 - distance / 2, x1 + distance / 2, x1 - distance / 2, t - 1);
    }

    //输出
    static void print(String[][] a,int b) {
        System.out.println("Case #"+b+":");
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {

                System.out.print(a[i][j]);
            }
            System.out.println();
        }
    }
}

Ac,运用递归的思想

3、熊熊找子串

题目地址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3364&konwledgeId=40

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

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String[] a = s.nextLine().split("");
        // 子串座标指针
        int k = 0;
        List<String> temps = new ArrayList<>();
        for (int i = 0; i < a.length;i++) {
            String temp = a[i];
            if (temps.indexOf(temp) == -1) {
                temps.add(temp);
            }
            if (i == a.length - 1) {
                break;
            }
            //要主要,这里是比较当前i指针与下一个是否相同,所以下面循环,要到i+1!
            if (a[i].equals(a[i + 1])) {
                String ktemp = "";
                //循环要到i+1
                for (int j = k; j <= i+1; j++) {
                    ktemp = ktemp + a[j];
                }
                if (!ktemp.equals("") && temps.indexOf(ktemp) == -1) {
                    temps.add(ktemp);
                }
            }else{
                //记住移动指针
                k = i+1;
            }
        }
        System.out.println(temps.size());
    }
}

4、路灯

题目地址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=1500&konwledgeId=134

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int l = scanner.nextInt();
        double max = 0;
        int[] a = new int[n+2];
        a[0] = 0;
        a[n+1] = l;
        for (int i = 1; i < n+1; i++) {
            a[i] = scanner.nextInt();
        }
        Arrays.sort(a);
        quiclSort(a,0,a.length-1);
        for(int i = 1; i < n+2;i++){
            if(i == 1 || i == n+1){
                if((a[i]-a[i-1])*2 > max){
                    max = (a[i]-a[i-1])*2;
                }
                continue;
            }
            if(a[i]-a[i-1] > max){
                max = a[i]-a[i-1];
            }
        }
        System.out.println(new DecimalFormat("#.00").format(max/2));
    }


    static void quiclSort(int[] a,int left,int right){
        if(left < right){
            int bb = onQuickSort(a, left, right);
            //以标兵为起点
            quiclSort(a,bb+1,right);
            //以标兵为终点
            quiclSort(a,left,bb-1);
        }
    }

    static int onQuickSort(int[] a,int left,int right){

        int bb = a[left];
        while(left < right){

            while(left < right && a[right] >= bb){
                right--;
            }
            swap(a, right, left);//标兵此时在right位置
            while(left < right && a[left] <= bb){
                left++;
            }
            swap(a, right, left);
        }
        return left;
    }

    static void swap(int[] a, int low,int hight){  
        int tmp = a[low];  
        a[low] = a[hight];  
        a[hight] = tmp;  
    } 

}

5、股神

题目地址:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=1664&konwledgeId=134

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        boolean isRising = true;
        int base = 1;
        int last = 1;
        for (int i = 1; i < n; i++) {
            if (!isRising) {
                isRising = true;
                last = last - 1;
                continue;
            }
            if (isRising) {
                for (int j = 0; j < base; j++) {
                    if(i>=n){
                        break;
                    }
                    last = last + 1;
                    i++;
                }
                isRising = false;
                i--;
                base++;
            }
        }
        System.out.println(last);
    }
}

下下周,回归android,写一写Activity的启动源码分析。

点赞