P1618 三连击(升级版) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MAX 5010 using namespace std; double a, b, c; int S[11], pos; bool vis[11]; void dfs(int k) { if(k > 9) { double aa = 0, bb = 0, cc = 0; for(int i = 1; i <= 3; i++) aa = aa * 10 + S[i]; for(int i = 4; i <= 6; i++) bb = bb * 10 + S[i]; for(int i = 7; i <= 9; i++) cc = cc * 10 + S[i]; if((bb/aa==b/a) && (cc/aa==c/a) && (cc/bb==c/b)) cout << aa <<" "<< bb <<" "<< cc << endl, pos = 1; return; } for(int i = 1; i <= 9; i++) { if(vis[i]) continue; S[k] = i; vis[i] = true; dfs(k + 1); vis[i] = false; } return; } int main() { cin >> a >> b >> c; dfs(1); if(pos == 0) cout << "No!!!" << endl; return 0; }
🏳️🌈🏳️🌈🏳️🌈
#include<iostream> using namespace std; int main() { int i,j,k,m,sum=0; int a[10]; for(i=123; i*3<=987; i++) { //由于数字不重复,故区间范围从123-987 for(m=1; m<10; m++) a[m]=0; //全部初始化为0 a[i/100]=1; a[i/10%10]=1; a[i%10]=1; j=i*2;//第二个三位数,与第一个三位数满足1:2的关系 a[j/100]=1; a[j/10%10]=1; a[j%10]=1; k=i*3;//第三个三位数,与第一个、第二个三位数满足1:2:3的关系 a[k/100]=1; a[k/10%10]=1; a[k%10]=1; for(m=1; m<10; m++) sum=sum+a[m]; if(sum==9) cout<<i<<" "<<j<<" "<<k<<endl;//若恰好为9,说明满足题设,输出 } return 0; }
Code over!