The 1,2,3......,9 consists of 3 three-digit abc,def and GHI, each of which is used exactly once, requiring abc:def:ghi=1:2:3. Two solutions for outputting all solutions

Source: Internet
Author: User
Tags array definition

Solution 1:

#include <iostream>  
#include <cstdio>  
using namespace std;  
  
void result (int num, int &result_add, int &result_mul)  
{  
    int i, j, K;  
   
    i = num/100;        Hundred  
    j = num/10%;    10-bit  
    k = num%;         Single-digit  
   
    Result_add = i + j + K;    Decomposition of the number of digits added  
    result_mul *= i * j * k;    Multiply  
}  
  
int main ()  
{  
    int i, j, K;  
    int Result_add, Result_mul;  
    for (i = 123 I <=329; i++)  
    {  
        J = i * 2;  
        K = i * 3;   
        Result_add = 0;  
        Result_mul = 1;  
        Result (I, Result_add, Result_mul);  
        Result (J, Result_add, Result_mul);  
        Result (k, Result_add, Result_mul);  
        if (Result_add = = && Result_mul = = 362880)  
            printf ("%d%d%d\n", I, J, K);  
    }  
    return 0;  
}  

Solution 2:

#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace std;  
  
int main (int argc, const char * argv[])  
{    
    int n, I, J;  
    The char a[10];//array definition is larger than the actual for   
    (n = 123; n < n++)  
    {  
        sprintf (A, "%d", n * 1000000 + N * 2 * 1000 + n * 3); /save Abcdefghi to string A for   
        (j = 0, i = ' 1 '; I <= ' 9 '; MEMCHR (A, i++, 9) && J + +);//Compare 1 to 9, record with J. When the character array A does not have the I corresponding character, according to the short-circuit principle, J + + does not execute, after the loop, J will not be equal to 9   
        if (j = = 9)   
        {  
            printf ("%d%d%d \ n", N, N * 2, n * 3);  
        }  
    }  
return 0;  
}  

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.