Charles_koyi @ 2023-08-04 11:40:19
#include<bits/stdc++.h>
using namespace std;
long long a,b,t,c,flag=0,ans,ans_2,ans_3;
bool num[101]={0},eve=0;
void koyi() //算最简比例
{
for(long long i=2;i*i<=a;)
{
if(a%i==0 and b%i==0 and c%i==0)
{
a=a/i;
b=b/i;
c=c/i;
}
else
{
i++;
}
}
}
int main()
{
cin>>a>>b>>c;
if(a>c) //排列a,b,c(从小到大)
{
swap(a,c);
}
if(a>b)
{
swap(a,b);
}
if(b>c)
{
swap(b,c);
}
if(a==0)
{
cout<<"No!!!"; //特判0
return 0;
}
koyi();
for(int i=1;i<=9;i++) //列举第一个数的可能
{
for(int j=1;j<=9;j++)
{
if(j!=i)
{
for(int q=1;q<=9;q++)
{
if(q!=j and q!=i)
{
ans=0;
flag=0;
ans_2=0;
ans_3=0;
memset(num,0,sizeof(num));
ans=i*100+j*10+q;
if(ans%a!=0)
{
continue;
}
num[i]=1;
num[j]=1;
num[q]=1;
ans_2=ans/a*b;
ans_3=ans/a*c;
if(ans_2>=1000 or ans_3>=1000)
{
continue;
}
for(int z=1;z<=3;z++)
{
t=ans_2%10;
if(num[t]==1)
{
flag=1;
break;
}
num[t]=1;
t=ans_3%10;
if(num[t]==1)
{
flag=1;
break;
}
ans_2=ans_2/10;
ans_3=ans_3/10;
num[t]=1;
}
if(flag==1)
{
continue;
}
cout<<ans<<" "<<ans/a*b<<" "<<ans/a*c<<endl;
eve=1;
}
}
}
}
}
if(eve==0)
{
cout<<"No!!!";
}
return 0;
}
by xzyNOIP @ 2023-08-04 11:44:40
这道题可以直接用全排列
直接用全排列来构造三个数,最后验证一下比例就可以了
核心代码如下
do
{
int n1,n2,n3;
n1=make(num[0],num[1],num[2]);
n2=make(num[3],num[4],num[5]);
n3=make(num[6],num[7],num[8]);
double l,ll,lll;
l=n1*1.0/a;
ll=n2*1.0/b;
lll=n3*1.0/c;
if(l==ll&&ll==lll)
{
cnt++;
cout<<n1<<" "<<n2<<" "<<n3<<endl;
}
}while(next_permutation(num,num+9));