拼多多笔试第一题

太难了,唯一Ac100%的题

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n= in.nextInt();
            int[][] arr=new int[n][n];
            solution(n,arr);
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; j++){
                    System.out.print(arr[i][j]+" ");
                }
                System.out.println();
            }
        }
    }

    private static void solution(int n, int[][] arr) {
        if (n%2==0){
            int left=0;
            int right =n-1;
            int top= 0;
            int bottom=n-1;
            int t=n-1;
            while (left<=right&&top <=bottom){
                for (int i=right;i>=left; i--){
                    if (i==right||i==left){
                        arr[top][i]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[top][i]=1;
                    }else {
                        arr[top][i]=2;
                    }
                }
                top++;
                for (int i=top;i<=bottom; i++){
                    if (i==bottom){
                        arr[i][left]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[i][left]=4;
                    }else {
                        arr[i][left]=3;
                    }
                }
                left++;
                for (int i=left;i<=right; i++){
                    if (i==right){
                        arr[bottom][i]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[bottom][i]=6;
                    }else {
                        arr[bottom][i]=5;
                    }
                }
                bottom--;
                for (int i=bottom;i>=top; i--){
                    if (i>(t)/2){
                        arr[i][right]=7;
                    }else {
                        arr[i][right]=8;
                    }
                }
                right--;
            }
        }else {
            int left=0;
            int right =n-1;
            int top= 0;
            int bottom=n-1;
            int t=n-1;
            while (left<=right&&top <=bottom){
                for (int i=right;i>=left; i--){
                    if (i==right||i==left||i==t/2){
                        arr[top][i]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[top][i]=1;
                    }else {
                        arr[top][i]=2;
                    }
                }
                top++;
                for (int i=top;i<=bottom; i++){
                    if (i==bottom||i==t/2){
                        arr[i][left]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[i][left]=4;
                    }else {
                        arr[i][left]=3;
                    }
                }
                left++;
                for (int i=left;i<=right; i++){
                    if (i==right||i==t/2){
                        arr[bottom][i]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[bottom][i]=6;
                    }else {
                        arr[bottom][i]=5;
                    }
                }
                bottom--;
                for (int i=bottom;i>=top; i--){
                    if (i==t/2){
                        arr[i][right]=0;
                        continue;
                    }
                    if (i>(t)/2){
                        arr[i][right]=7;
                    }else {
                        arr[i][right]=8;
                    }
                }
                right--;
            }
        }

    }
}
#笔试题目##拼多多#
全部评论
老哥。太暴力了
点赞 回复 分享
发布于 2020-09-01 21:15
我也只会一个
点赞 回复 分享
发布于 2020-09-01 21:19

相关推荐

02-11 12:20
门头沟学院 Java
面试中的青提很胆小:我不信有比我们学校更逆天的,计算机专业就业第一位是我们学校二餐厅的打印店
点赞 评论 收藏
分享
bLanK的小号:建议自己写一个比较新颖的项目,比如思维导图,在线文档,仿造postman,仿造一个组件库
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务