题意:给出一个n,然后要得到所有的s1和s2满足,s1与s2都是位数不存在相等的数字,且s1 / s2 = n。
题解:纯暴力题,枚举s2,计算s1,然后判断s1和s2是否都是不重复数位的数字,终止条件是s1 > 9876543210。
#include <stdio.h> long long n; int vis[10]; bool judge(long long x) { if (x > 9876543210) return false; for (int i = 0; i < 10; i++) vis[i] = 0; while (x > 0) { int temp = x % 10; if (vis[temp]) return false; vis[temp] = 1; x /= 10; } return true; } void dfs(long long cur, long long num) { if (num > 9876543210) return; if (judge(cur) && judge(num)) printf("%lld / %lld = %lld\n", num, cur, n); dfs(cur + 1, (cur + 1) * n); } int main() { int t; scanf("%d", &t); while (t--) { scanf("%lld", &n); dfs(1, n); if (t) printf("\n"); } return 0; }
原文地址:http://blog.csdn.net/hyczms/article/details/45154471