思路
一层一层地染:比如说对
1 2 3
2 1 2
3 2 1
第一层是
1 2 3
2
3
第二层是
1 2
2
第三层是
1
$\color{Cyan}{由于这道题只是一道简单的语法题,所以我用三种语言写了这道题当作练手~}$
C++
#include
using namespace std;
const int N = 105;
int mp[N][N];
void paint(int n,int st){
for(int i=st;i<=st+n-1;i++)
mp[i][st]=i-st+1;
for(int i=st;i<=st+n-1;i++)
mp[st][i]=i-st+1;
return;
}
void solve(int n){
for(int i=1;i<=n;i++)
paint(n+1-i,i);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<
}
cout<
}
cout<
return;
}
int main(){
int n;
while(cin>>n, n){
solve(n);
}
return 0;
}
Java
import java.util.Scanner;
public class Main {
static final int N = 1005;
static int[][] mp = new int[N][N];
static void paint(int n, int st) {
for (int i = st; i <= st + n - 1; i++)
mp[i][st] = i - st + 1;
for (int i = st; i <= st + n - 1; i++)
mp[st][i] = i - st + 1;
}
public static void solve(int n) {
for (int i = 1; i <= n; i++)
paint(n + i - 1, i);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(mp[i][j]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int n;
n = in.nextInt();
if (n == 0) break;
solve(n);
}
}
}
Python
mp = [[0 for i in range(105)] for j in range(105)]
def paint(n, st):
for i in range(st, st + n - 1 + 1):
mp[i][st] = i - st + 1
for i in range(st, st + n - 1 + 1):
mp[st][i] = i - st + 1
def solve(n):
for i in range(1, n + 1):
paint(n - i + 1, i)
for i in range(1, n + 1):
for j in range(1, n + 1):
print(mp[i][j], end=" ")
print()
print()
while True:
n = int(input())
if n == 0:
break
solve(n)