2차원 배열 예제

// 1 4 7
// 2 5 8
// 3 6 9

#include <stdio.h>
int main() {
int arr[100][100];
int n, i, j;
int column = 0, input = 1;
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
arr[j][column] = input++;
}
column++;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
return 0;
}
// 1 6 7
// 2 5 8
// 3 4 9

#include <stdio.h>
int main() {
int arr[100][100];
int n, i, j, input = 1;
scanf("%d", &n);
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
for (j = 0; j < n; j++) {
arr[j][i] = input++;
}
}
else {
for (j = n - 1; j >= 0; j--) {
arr[j][i] = input++;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
return 0;
}
// 1 2 3
// 6 5 4
// 7 8 9

#include <stdio.h>
int main() {
int arr[100][100];
int n, i, j, input = 1;
scanf("%d", &n);
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
for (j = 0; j < n; j++) {
arr[i][j] = input++;
}
}
else {
for (j = n - 1; j >= 0; j--) {
arr[i][j] = input++;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%3d", arr[i][j]);
}
printf("\n");
}
return 0;
}
Total views

댓글

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×