Exercise 2-10 Arrangement (permutation)
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. Tip: Don't have to be too much of a brain.
Problem Solving Ideas:
- First, the ABC minimum can only be 123, the maximum value of 329, to comply with test instructions.
- The key to the problem is that each number in 1-9 must appear and cannot be duplicated. Workaround: Use the array a[1],.., a[9] respectively to indicate whether 1,2...,9 appear, the occurrence is recorded as 1, otherwise recorded as 0, only need to judge a[1]+...+a[9] = = 9, if true, then each number appears, and does not repeat.
The code is as follows:
#include <stdio.h> #include <string.h>int main () {int I, Count = 0, J, K, A[10], c;//with I for abc,j means def,k Ghi T (A, 0, sizeof (a));//Initialize the array, all zeros for (i=123; i<=329; i++) {j = i*2;k = i*3;a[i/100] = a[i/10%10] = a[i%10] = 1;//separate marks appear Number a[j/100] = a[j/10%10] = a[j%10] = 1;a[k/100] = a[k/10%10] = a[k%10] = 1;for (c=0; c<10; C + +) Count + = A[c];if (c Ount = = 9)//only 1 to 9 each digit is marked with a result of 9 printf ("%d%d%d\n", I, J, k); count = 0;memset (A, 0, sizeof (a));} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithmic competition Getting Started classic exercise 2-10 arrangement (permutation)