public class test { public static void main(String[] args) { long a = 4; for (long i = 0; i < 2 * a - 1; i++) { for (long j = 0; j < 2 * a - 1; j++) { long m = Math.abs(i - a + 1) + 1; long n = Math.abs(j - a + 1) + 1; long x = m > n ? m : n; System.out.print(x); } System.out.println(); } } }
public class Test { public static void main(String[] args)throws Exception{ BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
while(true){ int num=Integer.parseInt(reader.readLine());
原来如此,他的代码我看得难懂,不知道他Math.abs里面还要减1干嘛 听你这么说中心我就突然觉悟了,还有中心不是(a,a)应该是(a-1,a-1) public static void main(String[] args){ int n = 4; int center = n - 1; int len = n * 2 - 1; int v; int nx,ny; for(int y = 0; y < len; y++){ for(int x = 0; x < len; x++){ nx = Math.abs(x - center) + 1; ny = Math.abs(y - center) + 1; v = nx > ny ? nx : ny; System.out.print(v); } System.out.println(); } }
public class RadiationMatrix { public static final int N = 5; public static void main(String[] args) { System.out.println("Init-N=" + N); int n = 2*N-1; int[][] prints = new int[n][n];
public class test {
public static void main(String[] args) {
long a = 4; for (long i = 0; i < 2 * a - 1; i++) { for (long j = 0; j < 2 * a - 1; j++) {
long m = Math.abs(i - a + 1) + 1;
long n = Math.abs(j - a + 1) + 1;
long x = m > n ? m : n;
System.out.print(x);
}
System.out.println();
}
}
}
public static void main(String[] args)throws Exception{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
while(true){
int num=Integer.parseInt(reader.readLine());
try{
if(num>0){
printMatrix(num);
}else{
break;
}
}catch(Exception e){
break;
}
}
reader.close();
}
public static void printMatrix(int n){
if(n<1) return;
int size=2*n-1;
int val;
System.out.print("\n--------------------------------------\n");
for(int i=1;i<=size;i++){
for(int j=1;j<=size;j++){
val=Math.max(Math.abs(i-n)+1,Math.abs(j-n)+1);
System.out.print(String.format("%-4s", val));
}
System.out.print('\n');
}
System.out.print("--------------------------------------\n");
}
}
解释一下就是矩阵中任意一点的坐标的x或者y距离矩阵中心点的距离较大的值。假设制定值为a,则矩阵有2a-1行,2a-1列,中心位置为(a,a)
则对于任意一点(x,y)
value=MAX(|x-a|,|y-a|);
听你这么说中心我就突然觉悟了,还有中心不是(a,a)应该是(a-1,a-1)
public static void main(String[] args){
int n = 4;
int center = n - 1;
int len = n * 2 - 1;
int v;
int nx,ny;
for(int y = 0; y < len; y++){
for(int x = 0; x < len; x++){
nx = Math.abs(x - center) + 1;
ny = Math.abs(y - center) + 1;
v = nx > ny ? nx : ny;
System.out.print(v);
}
System.out.println();
} }
你这个他啊你啊的,把我都看蒙了。俺就是一个人好不
至于中心点的位置,我有我自己的理解方式,在我的脑子里面得到的结果的索引值为从1开始,
而print的时候是从零开始,所以才有了abs里面的+1,
说到底你把你的center带入的话,咱们的程序完全一样吧。呵呵
不过按照你说的中心为a-1,a-1可能更好理解一些。因为可以放在一套坐标系中来考虑。
public class RadiationMatrix {
public static final int N = 5;
public static void main(String[] args) {
System.out.println("Init-N=" + N);
int n = 2*N-1;
int[][] prints = new int[n][n];
for(int i=1; i<=N; i++) {
for(int j=0; j<2*i-1; j++) {
prints[N-i][N-i] = i;
for(int k=0; k<=2*(i-1);k++) {
prints[N-i][N-i+k] = i;
prints[N-i+k][N-i] = i;
prints[N-i+2*(i-1)][N-i+k] = i;
prints[N-i+k][N-i+2*(i-1)] = i;
}
}
}
for(int i=0; i<prints.length; i++) {
for(int j=0; j<prints[i].length; j++) {
System.out.print(prints[i][j] + "\t");
}
System.out.println();
}
}
}
//Result
//Init-N=5
//5 5 5 5 5 5 5 5 5
//5 4 4 4 4 4 4 4 5
//5 4 3 3 3 3 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 2 1 2 3 4 5
//5 4 3 2 2 2 3 4 5
//5 4 3 3 3 3 3 4 5
//5 4 4 4 4 4 4 4 5
//5 5 5 5 5 5 5 5 5
for(int i=1; i<=N; i++) {
prints[N-i][N-i] = i;
for(int k=0; k<=2*(i-1);k++) {
prints[N-i][N-i+k] = i;
prints[N-i+k][N-i] = i;
prints[N-i+2*(i-1)][N-i+k] = i;
prints[N-i+k][N-i+2*(i-1)] = i;
}
}晕,多写了一个循环
这样就可以出来了
请参见
http://blog.csdn.net/icy_csdn/archive/2010/03/09/5359153.aspx