//给定一个数字n我想为它生成相应的二维矩阵。 //例如,对于n = 1,我的2-D矩阵应该是
对于n = 1
1 2 3 4
表示n = 2
// TODO: mount the tigers route with a a new router just for tigers
// exactly like lions below
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var _ = require('lodash');
var morgan = require('morgan');
var lionRouter = require('./lions');
var tigerRouter = require('./tigers');
app.use(morgan('dev'))
app.use(express.static('client'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// this is called mounting. when ever a req comes in for
// '/lion' we want to use this router
app.use('/lions', lionRouter);
app.use(function(err, req, res, next) {
if (err) {
res.status(500).send(error);
}
});
app.listen(3000);
console.log('on port 3000');
对于n = 3
1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16
答案 0 :(得分:2)
使用递归可以解决问题。例如,下面的代码精确打印给定import java.util.Scanner;
public class Main {
public static void main(final String[] args) {
final Scanner scanner = new Scanner(System.in);
final int n = scanner.nextInt();
final int[][] matrix = create(1, (int) Math.pow(2, n));
print(matrix);
}
private static int[][] create(final int startValue, final int size) {
if (size == 1) {
return new int[][]{{startValue}};
} else {
final int half = size / 2;
final int step = half * half;
return combine(create(startValue, half), create(startValue + step, half),
create(startValue + 2 * step, half), create(startValue + 3 * step, half));
}
}
private static int[][] combine(final int[][] m1, final int[][] m2, final int[][] m3, final int[][] m4) {
final int initialSize = m1.length;
final int sizeOfResult = initialSize * 2;
final int[][] result = new int[sizeOfResult][sizeOfResult];
for (int row = 0; row < initialSize; row++) {
for (int col = 0; col < initialSize; col++) {
result[row][col] = m1[row][col];
result[row][col + initialSize] = m2[row][col];
result[row + initialSize][col] = m3[row][col];
result[row + initialSize][col + initialSize] = m4[row][col];
}
}
return result;
}
private static void print(final int[][] matrix) {
for (final int[] row : matrix) {
for (final int val : row) {
System.out.printf("%-5d", val);
}
System.out.println();
}
}
}
所需的矩阵。
"test:ci": ...
"test:units": ....
"test:integration"...