[ create a new paste ] login | about

Link: http://codepad.org/bnnJe4MA    [ raw code | output | fork ]

C, pasted on Jul 9:
#include <stdio.h>

int main(void)
{
	int h, x = 0;
	int i = 0;
	int j = 0;
	int m[100][100]={0};

	printf("奇数を入力してください:");
	scanf("%d", &x);
	j = x / 2;
	m[0][x / 2] = 1;

	for (h = 2; h <= x * x; h++) {
		i = i - 1;
		j = j + 1;

		if (i < 0 && j > x - 1){
			i += 2;
			j -= 1;
		}
		else if (i < 0)
			i += x;
		else if (j > x - 1)
			j -= x;
		else if (m[i][j] != 0){
			i += 2;
			j -= 1;
		}
		m[i][j] = h;
	}

    for (i = 0; i < x; i++) {
        for (j = 0; j < x; j++)
            printf("%4d", m[i][j]);
        putchar('\n');
    }

    return (0);
}


Output:
1
奇数を入力してください:


Create a new paste based on this one


Comments: