java二维数组行列转换,java-将二维数组从行转换为块

我试图在这段代码中工作数周.

我需要将二维数组中的行和列转换为块.

它应该在大小为n * n的任何矩阵上工作. (已经给了我数组的大小)

例如:

这个:

int[][] input = {{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9},

{1,2,3,4,5,6,7,8,9}} ;

this is need to be the output:

{{1,2,3,1,2,3,1,2,3}

{4,5,6,4,5,6,4,5,6}

{7,8,9,7,8,9,7,8,9}

{1,2,3,1,2,3,1,2,3}

{4,5,6,4,5,6,4,5,6}

{7,8,9,7,8,9,7,8,9}

{1,2,3,1,2,3,1,2,3}

{4,5,6,4,5,6,4,5,6}

{7,8,9,7,8,9,7,8,9}}

我总是停留在某个地方.

那就是我写的代码:

sqrtN is the size.

在上述情况下,此处的sqrtN为3.

` public static int[][] blocks(int[][] matrix, int sqrtN) {

int[][] blocks = matrix;

int i = 0;

int counter = 1;

while(counter+1

int n = sqrtN;//"n" will the size of the matrix.

while(n

int t = counter;

int j = 0;

while(t

int k = n;

if(t==counter & i%n==0 & i>0)

j= t-1;

if(j%sqrtN==0)

i = 0;

while(k==n || k%sqrtN!=0){

int temp = blocks[t][i];

blocks[t][i] = blocks[j][k];

blocks[j][k] = temp;

i= i+1;

k= k+1;

}

j=j+sqrtN;

t=t+sqrtN;

}

n= n+sqrtN;

counter= counter+1;

}

i=counter;

}

return blocks;`

我真的很高兴找到问题的答案.

多谢你们.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值