Luogu-P1618 Triple Strike (Upgraded Edition)

Keywords: C++

Title Description

Will 1, 2,... Nine digits in total are divided into three groups, each of which consists of three triple digits, and the ratio of the three triple digits is A:B:C. Try to find all three triple digits satisfying the conditions. If there is no solution, output "No!!!".

Input format

Three numbers, A B C.

Output format

Several rows, three digits per row. Arrange in ascending order according to the first number in each line.

Input and Output Samples

Input #1

1 2 3

Output #1

192 384 576
219 438 657
273 546 819
327 654 981

Note/hint

Guarantee A<B<C

Topic Essentials and Analysis

The requirement of the title is that three of the nine numbers are arranged in a group to form three triple digits, and the proportion of A:B:C should be satisfied.

Because these three numbers cannot be selected repeatedly, the minimum three-digit number can be obtained as 123. So we can solve the problem of violence from 123. There are two small details as follows:

  1. The three digits obtained cannot contain the number 0.
  2. The three-digit element itself cannot be repeated.

Code

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
    int q,b,c,sum=0,temp;
    cin >> q >> b >> c;
    for(int i=123;i<999;i++)
    {
        int a[10]={0},s=0;
        if(i/q*c>999)
            break;
        if(i%q!=0)
            continue;
        temp = i;//The first number judgment begins.
        for(int n=0;n<3;n++)
        {
            if(temp%10==0||a[temp%10]==1)
            {
                s=1;
                break;
            }

            a[temp%10]=1;
            temp = temp / 10;

        }
        if(s==1) continue;
        int j=i/q*b;
        temp = j;//The second number judgement begins.
        for(int n=0;n<3;n++)
        {
            if(a[temp%10]==0&&temp%10!=0)
                a[temp%10]=1;
            else
            {
                s=1;
                break;
            }
            temp = temp/10;
        }
        if(s==1) continue;
        int k=i/q*c;
        temp = k;//The third number judgment begins.
        for(int n=0;n<3;n++)
        {
            if(a[temp%10]==0 && temp%10!=0)
                a[temp%10]=1;
            else
            {
                s=1;
                break;
            }
            temp = temp/10;
        }
        if(s==1) continue;
        else
        {
            cout << i << " " << j << " " << k <<endl;
            sum++;
        }
    }
    if(sum==0)
    {
        cout << "No!!!" <<endl;
    }
    return 0;
}

For more information, you can visit my personal blog: A big monster

Posted by toshesh on Mon, 30 Sep 2019 15:17:14 -0700