[ create a new paste ] login | about

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

C, pasted on Apr 24:
#include <stdio.h> 
void clear_array(int *a, int size) {
	int i;
	for (i = 0; i < size; i++) a[i] = 0;
}
void init_vertices(int *vertices, int size) {
	clear_array(vertices, size);
	if (0 < size) vertices[0] = 1;
	if (4 < size) vertices[4] = 2;
	if (9 < size) vertices[9] = 3;
}
void print_array(int *a, int size) {
	int i;
	for (i = 0; i < size; i++) printf("%d ", a[i]);
	puts("");
}
void print_star(int *vertices, int size) {
	printf("   %2d\n", vertices[0]);
	printf("%2d %2d %2d %2d\n", vertices[1], vertices[2], vertices[3], vertices[4]);
	printf(" %2d   %2d\n", vertices[5], vertices[6]);
	printf("   %2d\n", vertices[7]);
	printf(" %2d   %2d\n", vertices[8], vertices[9]);
}
int main() {
#define SIZE(a) (sizeof a / sizeof a[0])
	/*
	   0
	1 2 3 4
	 5   6
	   7
	 8   9
	*/
	int vertices[10];
	init_vertices(vertices, SIZE(vertices));
#define SUM(a, b, c, d) (vertices[a] + vertices[b] + vertices[c] + vertices[d])
	int bucket[15];
#define LOOP(vi) for (vertices[vi] = 4; vertices[vi] <=12; vertices[vi]++)
#define ISUSE(vi) bucket[vertices[vi]]
#define CONTINUE_OR_CHECK(vi) if (ISUSE(vi)) continue; else ISUSE(2) = 1;
#define CLEAR_AND_CHECK(vi) clear_array(bucket, SIZE(bucket)); ISUSE(vi) = 1;
	int sum;
	// 0, 4, 9番は固定
	LOOP(1) { CLEAR_AND_CHECK(1);
		LOOP(2) { CONTINUE_OR_CHECK(2);
			LOOP(3) { CONTINUE_OR_CHECK(3);
				LOOP(5) { CONTINUE_OR_CHECK(5);
					LOOP(6) { CONTINUE_OR_CHECK(6);
						LOOP(7) { CONTINUE_OR_CHECK(7);
							LOOP(8) { CONTINUE_OR_CHECK(8);
								sum = SUM(1,2,3,4);
								if (sum == SUM(4,6,7,8) && sum == SUM(8,5,2,0) && sum == SUM(0,3,6,9) && sum == SUM(9,7,5,1)) {
									printf("sum = %d, ", SUM(1,2,3,4));
									print_array(vertices, SIZE(vertices));
									print_star(vertices, SIZE(vertices));
									break;
								}
							}
						}
					}
				}
			}
		}
	}
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sum = 22, 1 4 5 11 2 9 7 6 7 3 
    1
 4  5 11  2
  9    7
    6
  7    3
sum = 24, 1 4 6 12 2 10 8 7 7 3 
    1
 4  6 12  2
 10    8
    7
  7    3
sum = 24, 1 6 4 12 2 10 8 5 9 3 
    1
 6  4 12  2
 10    8
    5
  9    3


Create a new paste based on this one


Comments: