题目
输入格式
三个数,A,B,C。
输出格式
若干行,每行 3 个数字。按照每行第一个数字升序排列。
输入输出样例
输入
1 2 3
输出
192 384 576
219 438 657
273 546 819
327 654 981
说明/提示
保证 A<B<C。
原题地址–>link
分析
首先,三个数都是三位数的数,其次,三个数内没有重复的数字。可能,我们首先想到的应该是回溯法,用回溯法去搜索合格的数。但是,我们都知道时间上会很浪费。所以,能不用回溯,就不用回溯,避免超时。这里用暴力枚举。
划重点 凡是见到,几个数,所有的数字不可以重复,都用一种叫做落位的方法(这个名字是我自己定的)。所谓的落位,就是Down下来,定义一个数组str[10],里边的初值都设置为0,用到哪个就令哪个的值为1;eg:str[i]=1;最后在满足数学条件的基础上再判断一下str的所有的值是不是都为1。(我敢说下边的代码是全网最容易看懂的,也是最简单的方法,如果有更加简洁的代码,请在下边留言哦,互相学习!)
Code
#include <iostream>
#include <cstring>
using namespace std;
bool JudgeOk(const int str[])//判断所有数没有重复,都用上了
{
for (int i = 1; i < 10; ++i) {
if(str[i]!=1) return false;
}
return true;
}
void Down(int n,int str[])//这个就是落位函数啦
{
while (n)
{
str[n%10]=1;
n/=10;
};
}
int main()
{
int str[10]={
0};
double a,b,c;
cin >> a >> b >> c;
int num2=0,num3=0,count=0;
for (int i = 100; i < 1000; ++i) {
num2=(int)((b*i)/a);
num3=(int)((c*i)/a);
Down(i,str);
Down(num2,str);
Down(num3,str);
if(JudgeOk(str)&&num2<1000&&num3<1000)
{
cout << i << " "<< num2 << " " << num3 << endl;
count++;
}
memset(str,0, sizeof(str));
}
if(count==0) cout << "No!!!";
return 0;
}