欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入
题目内容:有个数据集dataset,名称为DS,内只有一个table,名称为TB,该table有x个行(rows),y个整型列(columns),写一个算法实现:将其按“从左到右、从上到下”的顺序,后退一位,第一位(rows(0),columns(0))由被移出的最后一位(rows(x-1),columns(y-1))填上。
例如(当x=3, y=3):
1 2 3 step 1 9 1 2 step 2 8 9 1
4 5 6 ————-> 3 4 5 ————–> 2 3 4 ……
7 8 9 6 7 8 5 6 7
package cn.link.test;
import java.util.*;
public class Suanfa {
static int x=0;
static int y=0;
static int action=0;
public static void go(int [][]a){
System.out.println();
int [][] b=new int[x][y];
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
int n=j-1;
int m=i;
if(n<0){
n=y-1;
m=i-1;
if(m<0){
m=x-1;
}
}
b[i][j]=a[m][n];
System.out.printf(“%5d”,b[i][j]);
}
System.out.println();
}
if(action<(x*y-1)){
action++;
go(b);
}
}
public static void main(String []args){
Scanner in=new Scanner(System.in);
x=in.nextInt();
y=in.nextInt();
int [][] a=new int[x][y];
int temp=0;
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
temp++;
a[i][j]=temp;
}
}
for(int i=0;i<x;i++){
for(int j=0;j<y;j++){
System.out.printf(“%5d”,a[i][j]);
}
System.out.println();
}
go(a);
}
}
3
3
1 2 3
4 5 6
7 8 9
9 1 2
3 4 5
6 7 8
8 9 1
2 3 4
5 6 7
7 8 9
1 2 3
4 5 6
6 7 8
9 1 2
3 4 5
5 6 7
8 9 1
2 3 4
4 5 6
7 8 9
1 2 3
3 4 5
6 7 8
9 1 2
2 3 4
5 6 7
8 9 1
1 2 3
4 5 6
7 8 9
成功生成(总时间:2 秒)
正如我总是意犹未尽的想起你。