Algorithmic competition Getting Started classic exercise 2-10 arrangement (permutation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.