滴滴2021后端开发岗笔试:
生成一个斐波那契数列的螺旋二维数组
import java.util.Scanner;
public class didi3 {
private static int[] arr;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n= scan.nextInt();
if(n==1){
System.out.println(1);
return ;
}
arr = getArray(n);
int[][] res = generata(n, arr);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(res[i][j]+" ");
}
System.out.println();
}
}
public static int[][] generata(int n , int[] arr ){
int l = 0,r = n-1, t = 0,b = n-1;
int[][] mat = new int[n][n];
int num = 1,tar = n * n ;
while (num <= tar ){
for(int i=l;i<=r;i++) {
mat[t][i] = arr[arr.length-num];
num++;
}
t++;
for(int i=t;i<=b;i++){
mat[i][r] = arr[arr.length-num];
num++;
}
r--;
for(int i=r;i>=l;i--){
mat[b][i] = arr[arr.length-num];
num++;
}
b--;
for(int i=b;i