Luo Gu -P1618 trifecta (upgrade version)

Title Description

The 1,2, ..., 9 9 number into three groups, each consisting of three three-digit number, and that the three three-digit ratio is A: B: C, again determined to meet the conditions of all three three digits, if no solution, output "No !!!".

Input Format

Three numbers, ABC.

Output Format

Several rows of three numbers. Each row arranged in a numerical order.

Sample input and output

Input # 1

1 2 3

Output # 1

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

Description / Tips

Ensure that A <B <C

Title and effect analysis

It is the subject of the request numbers 1 to 9, nine three atoms arranged in a group, consisting of three three digits, and the need to satisfy A: C ratio: B.

Since this number can not be repeated three selected, it is possible to obtain a three-digit number is 123 minimum. So you can start from 123 to solve the violence, there are two small details are as follows:

  1. The resulting three-digit 0 not contain this number.
  2. The three-digit element itself can not 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;//第一个数判断开始;
        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;//第二个数判断开始;
        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;//第三个数判断开始;
        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;
}

More you can visit my personal blog: a much blame

Guess you like

Origin www.cnblogs.com/cydi/p/11455669.html