題目描述
將1,2,…,9共9個數分成三組,分別組成三個三位數,且使這三個三位數的比例是A:B:C,試求出所有滿足條件的三個三位數,若無解,輸出“No!!!”。
輸入格式
三個數,A B C。
輸出格式
若干行,每行3個數字。按照每行第一個數字升序排列。
輸入輸出樣例
輸入 #1
1 2 3
輸出 #1
192 384 576
219 438 657
273 546 819
327 654 981
說明/提示
保證A<B<C
題目大意及分析
題目要求就是在1~9九個數中三個數為一組排列,組成三個三位數,並且需要滿足 A:B:C 的比例。
因為這三個數不能重復選,所以可以得到最小的一個三位數為123。所以就可以從123開始暴力求解了,有兩個小細節如下:
- 所得到的三位數中不能含有0這個數。
- 這個三位數自身元素也不能夠重復。
代碼
#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;
}