Find such two data: 5 digits = 2 * 4 digits, 9 digits are different

Keywords: C++

First, write to judge whether a five digit number and a four digit number are the same
Using the method of mark array to judge
Return 1 if different, return 0 if same

int panbie(int x, int y)
{
    int i, m, n;
    int a[10] = { 0 };//Define tag array
    for (i = 0; i <= 4; i++)
    {
        n= x % 10;
        a[n]++;//Array marking
        x = x/ 10;//
    }
    for (i = 0; i <= 3; i++)
    {
        n = y % 10;
        a[n]++;//Array marking
        y = y/ 10;
    }

    for (i = 0; i <= 9;i++)
    if (a[i] >= 2)
        return 0;
    return 1;
}

The main function circulates through all five and four digits

for (wu = 10000; wu < 100000;wu++)
    for (si = 1000; si < 10000; si++)

Find out the five and four digits that meet the question conditions when traversing

if (wu == (2 * si))
        {
            i = panbie(wu, si);//Call judgment function
            if (i == 1)
                printf("%d=2*%d\n", wu, si);
        }

Output after meeting the conditions
The complete source code is as follows

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int panbie(int x, int y)
{
    int i, m, n;
    int a[10] = { 0 };
    for (i = 0; i <= 4; i++)
    {
        n= x % 10;
        a[n]++;
        x = x/ 10;//
    }
    for (i = 0; i <= 3; i++)
    {
        n = y % 10;
        a[n]++;
        y = y/ 10;
    }

    for (i = 0; i <= 9;i++)
    if (a[i] >= 2)
        return 0;
    return 1;

}

int main()
{
    int wu, si,i;
    for (wu = 10000; wu < 100000;wu++)
    for (si = 1000; si < 10000; si++)
    {
        if (wu == (2 * si))
        {
            i = panbie(wu, si);
            //printf("%d\n", i);
            if (i == 1)
                printf("%d=2*%d\n", wu, si);
        }

    }
    system("pause");
    return 0;
}

Operation result

10476=2*5238
10478=2*5239
10728=2*5364
10764=2*5382
10784=2*5392
10872=2*5436
10972=2*5486
12708=2*6354
12870=2*6435
12970=2*6485
13458=2*6729
13584=2*6792
13704=2*6852
13854=2*6927
14538=2*7269
14586=2*7293
14658=2*7329
15384=2*7692
15846=2*7923
15864=2*7932
16470=2*8235
16704=2*8352
17046=2*8523
17064=2*8532
17092=2*8546
17290=2*8645
17304=2*8652
18470=2*9235
18534=2*9267
18546=2*9273
18654=2*9327
18704=2*9352
 Please press any key to continue

Posted by tha_mink on Fri, 06 Dec 2019 14:20:57 -0800