Exercise 2-10
The 1,2,3,....,9 consists of 3 three-digit abc,def and GHI, each using exactly once, requiring abc:def:ghi=1:2:3. Outputs all solutions.
#include <stdio.h> #include <stdlib.h> #include <string.h>int main (int argc, char *argv[]) { int ABC, Def, Ghi; int a[10], count = 0; memset (A, 0, sizeof (a)); printf ("n\n"); for (ABC = 123; ABC <= 329; abc++) { def = 2*abc; Ghi = 3*ABC; A[ABC/100] = a[abc/10%10] = a[abc%10] = 1; A[DEF/100] = a[def/10%10] = a[def%10] = 1; A[GHI/100] = a[ghi/10%10] = a[ghi%10] = 1; int i; for (i = 1; I <= 9; i++) count + = A[i]; if (count = = 9) printf ("%d%d%d\n", ABC, Def, Ghi); Count = 0; memset (A, 0, sizeof (a)); System ("PAUSE"); return 0;}
Summary: 1 will be all possible numbers as a one-dimensional array subscript, and finally determine whether the sum is 9, if less than 9, there will be coincident, whereas each number has only one
2 after judging, Count and array are zeroed.
Algorithmic competition Getting started classic exercise 2-10 arrangement (permutation)