public static int[][] split(int[] integerArray){
//存放二维数组里每一个数组的长度
int [] sz = new int [integerArray.length];
int num=0, i = 1;
//获取二维数组里每一个数组的长度
ifor:for(; i<=integerArray.length; i++){
if(num == integerArray.length-1){
break;
}
jfor:for(int j=num;j<integerArray.length;j++){
if(j==integerArray.length-1){
sz[i-1]=j+1-num;
break ifor;
}
if(integerArray[j+1]-integerArray[j]!=1){
sz[i-1]=j+1-num;
num=j+1;
break jfor;
}
}
}
//设置二维数组里每一个一维数组的长度
int [][] ewsz= new int [i][];
for(int x=0; x<i; x++){
ewsz[x]= new int [sz[x]];
}
//分离数组integerArray
int k=0;
for(int x =0; x<ewsz.length; x++){
for(int y =0; y<ewsz[x].length; y++){
if(x==0){
ewsz[x][y]=integerArray[y];
}else{
ewsz[x][y]=integerArray[y+k];
}
}
k=k+ewsz[x].length;
}
return ewsz;
}
#2
第二题有会的吗
#3
你第二题没看懂,样例打错了还是?
#4
第二道,方法比较笨
import java.util.Scanner;
public class SpiralArray {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[][] a = new int[n][n];
s.close();
int len = n - 1;
int start = 0;
for (int i = 0; i <= n / 2; i++) {
for (int j = i; j < len + i; j++) {
a[i][j] = start - i + j + 1;
a[j][n - i - 1] = a[i][j] + len;
a[n - i - 1][n - j - 1] = a[i][j] + len * 2;
a[n - j - 1][i] = a[i][j] + len * 3;
}
start += len * 4;
len -= 2;
}
if(n % 2 != 0){
a[n/2][n/2] = n * n;
}
for (int[] is : a) {
for (int i : is) {
System.out.print(i + " " + "\t");
}
System.out.println();
}
}
}
#5
public static void test(int num) {
int[][] array = new int[num][num];
int i = 0, j = 0, k = 1, m = 1;
for(int n = 0; n < num * num; n++){
array[i][j] = n + 1;
if (k % 2 != 0){
if (j + m > num - 1 - k / 4 || j + m < k / 4){
k++;
i += m;
continue;
}
j += m;
}else{
if (i + m > num - 1 - k / 4 || i + m < k / 4){
k++;
m = -m;
j += m;
continue;
}
i += m;
}
}
for(int s = 0; s < num; s++){
System.out.println(Arrays.toString(array[s]));
}
}
public static void test(int num) {
int[][] array = new int[num][num];
int i = 0, j = 0, k = 1, m = 1;
for(int n = 0; n < num * num; n++){
array[i][j] = n + 1;
if (k % 2 != 0){
if (j + m > num - 1 - k / 4 || j + m < k / 4){
k++;
i += m;
continue;
}
j += m;
}else{
if (i + m > num - 1 - k / 4 || i + m < k / 4){
k++;
m = -m;
j += m;
continue;
}
i += m;
}
}
for(int s = 0; s < num; s++){
System.out.println(Arrays.toString(array[s]));
}
}
public static int[][] split(int[] integerArray){
//存放二维数组里每一个数组的长度
int [] sz = new int [integerArray.length];
int num=0, i = 1;
//获取二维数组里每一个数组的长度
ifor:for(; i<=integerArray.length; i++){
if(num == integerArray.length-1){
break;
}
jfor:for(int j=num;j<integerArray.length;j++){
if(j==integerArray.length-1){
sz[i-1]=j+1-num;
break ifor;
}
if(integerArray[j+1]-integerArray[j]!=1){
sz[i-1]=j+1-num;
num=j+1;
break jfor;
}
}
}
//设置二维数组里每一个一维数组的长度
int [][] ewsz= new int [i][];
for(int x=0; x<i; x++){
ewsz[x]= new int [sz[x]];
}
//分离数组integerArray
int k=0;
for(int x =0; x<ewsz.length; x++){
for(int y =0; y<ewsz[x].length; y++){
if(x==0){
ewsz[x][y]=integerArray[y];
}else{
ewsz[x][y]=integerArray[y+k];
}
}
k=k+ewsz[x].length;
}
return ewsz;
}
#2
第二题有会的吗
#3
第二题有会的吗
你第二题没看懂,样例打错了还是?
#4
第二道,方法比较笨
import java.util.Scanner;
public class SpiralArray {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[][] a = new int[n][n];
s.close();
int len = n - 1;
int start = 0;
for (int i = 0; i <= n / 2; i++) {
for (int j = i; j < len + i; j++) {
a[i][j] = start - i + j + 1;
a[j][n - i - 1] = a[i][j] + len;
a[n - i - 1][n - j - 1] = a[i][j] + len * 2;
a[n - j - 1][i] = a[i][j] + len * 3;
}
start += len * 4;
len -= 2;
}
if(n % 2 != 0){
a[n/2][n/2] = n * n;
}
for (int[] is : a) {
for (int i : is) {
System.out.print(i + " " + "\t");
}
System.out.println();
}
}
}
#5
public static void test(int num) {
int[][] array = new int[num][num];
int i = 0, j = 0, k = 1, m = 1;
for(int n = 0; n < num * num; n++){
array[i][j] = n + 1;
if (k % 2 != 0){
if (j + m > num - 1 - k / 4 || j + m < k / 4){
k++;
i += m;
continue;
}
j += m;
}else{
if (i + m > num - 1 - k / 4 || i + m < k / 4){
k++;
m = -m;
j += m;
continue;
}
i += m;
}
}
for(int s = 0; s < num; s++){
System.out.println(Arrays.toString(array[s]));
}
}
public static void test(int num) {
int[][] array = new int[num][num];
int i = 0, j = 0, k = 1, m = 1;
for(int n = 0; n < num * num; n++){
array[i][j] = n + 1;
if (k % 2 != 0){
if (j + m > num - 1 - k / 4 || j + m < k / 4){
k++;
i += m;
continue;
}
j += m;
}else{
if (i + m > num - 1 - k / 4 || i + m < k / 4){
k++;
m = -m;
j += m;
continue;
}
i += m;
}
}
for(int s = 0; s < num; s++){
System.out.println(Arrays.toString(array[s]));
}
}