【入门】拐角I
Description
输入整数N,输出相应方阵。
Input
一个整数N。( 0 < n < 10 )
Output
一个方阵,每个数字的场宽为3。
Sample Input 1
5
Sample Output 1
1 1 1 1 1 1 2 2 2 2 1 2 3 3 3 1 2 3 4 4 1 2 3 4 5
Source
二维数组
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
cin>>n;
int arr[n][n]={0};
int i=0,j=0;
while(i<n){
j=i;
while(j<n){
arr[i][j]=i;
j++;
}
j=i;
while(j<n){
arr[j][i]=i;
j++;
}
i++;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%3d",arr[i][j]+1);
}
cout<<endl;
}
return 0;
}
/********************************************************
tips:
1.数组角标从0开始,输出时要加1。
2.j既代表行,也代表列。
3.用printf时用%3d。
********************************************************/