Document

2次元データ処理
1
九九
• 以下の様な九九の表を作成
1 2 3 4 5 6 7 8
2 4 6 8 10 12 14 16
3 6 9 12 15 18 21 24
4 8 12 16 20 24 28 32
5 10 15 20 25 30 35 40
6 12 18 24 30 36 42 48
7 14 21 28 35 42 49 56
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72
9
18
27
36
45
54
63
72
81
2
void main(){
int x, y;
for(y=1; y<=9; y++){
for(x=1; x<=9; x++){
printf("%2d ", x*y);
}
printf("\n");
}
}
3
2次元配列を2次元平面に表示
int a[3][3];
a[0][0] = 0; a[0][1] = 1; a[0][2] = 2;
a[1][0] =10; a[1][1] =11; a[1][2] =12;
a[2][0] =50; a[2][1] =51; a[2][2] =52;
a[1][0]を表示
0 1 2
10 11 12
50 51 52
a[0][2]を表示
a[2][1]を表示
4
void main(){
int a[3][3], x, y;
a[0][0] = 0; a[0][1] = 1; a[0][2] = 2;
a[1][0] =10; a[1][1] =11; a[1][2] =12;
a[2][0] =50; a[2][1] =51; a[2][2] =52;
for(y=0; y<3; y++){
for(x=0; x<3; x++){
printf("%2d ", a[y][x]);
}
printf("\n");
}
}
5
○× (2次元平面に表示)
int a[3][3];
a[0][0] = 2; a[0][1] = 0; a[0][2] = 2;
a[1][0] = 0; a[1][1] = 1; a[1][2] = 0;
a[2][0] = 1; a[2][1] = 0; a[2][2] = 0;
a[0][2]を表示
a[1][0]を表示
× ・ ×
・ ○ ・
○ ・ ・
0なら・を表示
1なら○を表示
2なら×を表示
a[2][1]を表示
6
void main(){
int a[3][3], x, y;
a[0][0] = 2; a[0][1] = 0; a[0][2] = 2;
a[1][0] = 0; a[1][1] = 1; a[1][2] = 0;
a[2][0] = 1; a[2][1] = 0; a[2][2] = 0;
for(y=0; y<3; y++){
for(x=0; x<3; x++){
if( a[y][x] == 0 ){
printf("・");
} else if( a[y][x] == 1 ){
printf("○");
} else if( a[y][x] == 2 ){
printf("×");
}
}
printf("\n");
}
}
7
○× (そろっているか調査)
int a[3][3];
a[0][0] = 1; a[0][1] = 2; a[0][2] = 1;
a[1][0] = 2; a[1][1] = 1; a[1][2] = 0;
a[2][0] = 1; a[2][1] = 2; a[2][2] = 0;
○ × ○
× ○ ・
○ × ・
8
void main(){
int a[3][3], x, y, cnt;
a[0][0] = 1; a[0][1] = 1; a[0][2] = 1;
a[1][0] = 2; a[1][1] = 2; a[1][2] = 0;
a[2][0] = 1; a[2][1] = 2; a[2][2] = 0;
for(y=0; y<3; y++){ // 横を確認
cnt = 0;
for(x=0; x<3; x++){
if( a[y][x] == 1 ){
cnt ++;
}
}
if( cnt == 3 ){
printf("縦座標%dにて,○が横にそろっている.\n", y);
}
}
}
9
void main(){
int a[3][3], x, y, cnt;
a[0][0] = 1; a[0][1] = 2; a[0][2] = 1;
a[1][0] = 2; a[1][1] = 1; a[1][2] = 0;
a[2][0] = 1; a[2][1] = 2; a[2][2] = 0;
cnt = 0;
for(x=0; x<3; x++){ // 斜めを確認
y = 2-x;
if( a[y][x] == 1 ){
cnt ++;
}
}
if( cnt == 3 ){
printf("右上がり斜めにて,○がそろっている\n");
}
}
10
数独 (表示)
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
6,0,0,1,9,5,0,0,0,
0,9,8,0,0,0,0,6,0,
8,0,0,0,6,0,0,0,3,
4,0,0,8,0,3,0,0,1,
7,0,0,0,2,0,0,0,6,
0,6,0,0,0,0,2,8,0,
0,0,0,4,1,9,0,0,5,
0,0,0,0,8,0,0,7,9
};
5 3 . . 7 . . . .
6 . . 1 9 5 . . .
. 9 8 . . . . 6 .
8 . . . 6 . . . 3
4 . . 8 . 3 . . 1
7 . . . 2 . . . 6
. 6 . . . . 2 8 .
. . . 4 1 9 . . 5
. . . . 8 . . 7 9
問題は, http://ja.wikipedia.org/wiki/%E6%95%B0%E7%8B%AC より引用 11
void main(){
int x, y;
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
(略)
0,0,0,0,8,0,0,7,9
};
for(y=0; y<9; y++){
for(x=0; x<9; x++){
if( 0 == a[y][x] ){
printf(" .");
} else {
printf("%2d", a[y][x]);
}
}
printf("\n");
}
}
12
数独 (表示)
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
6,0,0,1,9,5,0,0,0,
0,9,8,0,0,0,0,6,0,
8,0,0,0,6,0,0,0,3,
4,0,0,8,0,3,0,0,1,
7,0,0,0,2,0,0,0,6,
0,6,0,0,0,0,2,8,0,
0,0,0,4,1,9,0,0,5,
0,0,0,0,8,0,0,7,9
};
5 3 .
. 7 .
. . .
6 . .
1 9 5
. . .
. 9 8
. . .
. 6 .
8 . .
. 6 .
. . 3
4 . .
8 . 3
. . 1
7 . .
. 2 .
. . 6
. 6 .
. . .
2 8 .
. . .
4 1 9
. . 5
. . .
. 8 .
. 7 9
13
int x, y;
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
(略)
0,0,0,0,8,0,0,7,9
};
for(y=0; y<9; y++){
if( y % 3 == 0 ){ printf("\n"); }
for(x=0; x<9; x++){
if( x % 3 == 0 ){ printf(" "); }
if( 0 == a[y][x] ){
printf(" .");
} else {
printf("%2d", a[y][x]);
}
}
printf("\n");
}
14
数独 (データの誤りを調査)
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
6,0,0,1,9,5,0,0,0,
0,9,8,0,0,0,0,6,0,
8,0,0,0,6,0,0,0,3,
4,0,0,8,0,3,0,0,1,
7,0,0,0,2,0,0,0,6,
0,6,0,8,0,0,2,8,0,
0,0,0,4,1,9,0,0,5,
0,0,0,0,8,0,0,7,9
};
5 3 .
. 7 .
. . .
6 . .
1 9 5
. . .
. 9 8
. . .
. 6 .
8 . .
. 6 .
. . 3
4 . .
8 . 3
. . 1
7 . .
. 2 .
. . 6
. 6 .
8 . .
2 8 .
. . .
4 1 9
. . 5
. . .
. 8 .
. 7 9
15
int x, y, n, cnt;
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
(略)
0,0,0,0,8,0,0,7,9
};
for(x=0; x<9; x++){
for(n=1; n<=9; n++){
cnt = 0;
for(y=0; y<9; y++){
if( a[y][x] == n ){ cnt ++; }
}
if( 1 < cnt ){
printf("%d %ds at x==%d.\n", cnt, n, x);
}
}
}
16
数独 (データの誤りを調査)
static int a[9][9] = {
5,3,0,0,7,0,0,0,0,
6,0,0,1,9,5,0,0,0,
0,9,8,0,0,0,0,6,0,
8,0,0,0,6,0,0,0,3,
4,0,0,8,0,3,0,0,1,
7,0,0,0,2,0,0,0,6,
0,6,0,8,0,0,2,8,0,
0,0,0,4,1,9,0,0,5,
0,0,0,0,8,0,0,7,9
};
5 3 .
. 7 .
. . .
6 . .
1 9 5
. . .
. 9 8
. . .
. 6 .
8 . .
. 6 .
. . 3
4 . .
8 . 3
. . 1
7 . .
. 2 .
. . 6
. 6 .
8 . .
2 8 .
. . .
4 1 9
. . 5
. . .
. 8 .
. 7 9
17
int x, y, xx, yy, n, cnt;
static int a[9][9] = {
(略)
};
for(xx=0; xx<3; xx++){
for(yy=0; yy<3; yy++){
for(n=1; n<=9; n++){
cnt = 0;
for(x=xx*3; x<(xx+1)*3; x++){
for(y=yy*3; y<(yy+1)*3; y++){
if( a[y][x] == n ){
cnt ++;
}
}
}
if( 1 < cnt ){
printf("%d %ds in block (%d,%d).\n", cnt, n, xx, yy);
}
}
}
}
18