AcWing 754. 平方矩阵 II
原题链接
困难
作者:
STU756
,
2021-01-20 01:07:32
,
所有人可见
,
阅读 401
//c++
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
int x;
while(scanf("%d", &n) && n != 0) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
x = abs(i - j) + 1; //打印的数其实就是离该行对角线上的数的距离+ 1
cout << x << " ";
}
cout << endl;
}
cout<< endl;
}
return 0;
}
//java
import java.util.Scanner;
public class Main{
public static void main(String...args) {
Scanner s = new Scanner(System.in);
int n, x;
while(true) {
n = s.nextInt();
if(n == 0) break;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
x = (int)Math.abs(i - j) + 1;
System.out.print(x);
if(j != n) System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
}
}