〇÷〇〇+ 〇÷〇〇+〇÷〇〇=1 ?

#include \u0026lt;iostream\u0026gt;#include \u0026lt;vector\u0026gt;using namespace std;void g_permutation(vector\u0026lt;int\u0026gt; \u0026amp;A, int i, int cur) { A = i; if (A.size() == cur) { cout \u0026lt;\u0026lt; A; for (int j = 1; j \u0026lt; A.size(); j++) cout \u0026lt;\u0026lt; " " \u0026lt;\u0026lt; A; cout \u0026lt;\u0026lt; endl; return; } for (int i = 0; i \u0026lt; A.size(); i++) { bool ok = true; for (int k = 0; k \u0026lt; cur; k++) { if (A == i) { ok = false; break; } } if (ok) g_permutation(A, i, cur); }}void print_permutation(int n) { vector\u0026lt;int\u0026gt; A(n, -1); for (int i = 0; i \u0026lt; n; i++) { int cur = 0; g_permutation(A, i, cur); }}int main() { int n; while (cin \u0026gt;\u0026gt; n) { print_permutation(n); } return 0;}全排列枚举嘛,前几天刚看。抄《算法竞赛入门经典》上的。
■网友
暴搜即可。放到http://ideone.com上0.1s出结果7 6 8 5 3 4 9 1 2 5 3 4 7 6 8 9 1 2 7 6 8 9 1 2 5 3 4 9 1 2 7 6 8 5 3 4 5 3 4 9 1 2 7 6 8 9 1 2 5 3 4 7 6 8 #include \u0026lt;iostream\u0026gt;#include \u0026lt;cstring\u0026gt;#include \u0026lt;cmath\u0026gt;using namespace std;bool valid(int (\u0026amp;b), int k){\tfor(int i=0; i\u0026lt;9 ;i++)\t\tif(b==k) \t\t\treturn false;\treturn true;}void solve(int (\u0026amp;b)){\tint p = -1;\tfor(int i=0; i\u0026lt;9 ;i++)\t{\t\tif(b==0) p=i;\t}\tif(p==-1)\t{\t\tdouble val = (double)b/(10*b+b)+ \t\t (double)b/(10*b+b)+ \t\t (double)b/(10*b+b);\t\tif( abs(val-1.0)\u0026lt;1e-10 ) \t\t{\t\t\tfor(int i=0; i\u0026lt;9 ; i++)\t\t\t\tcout \u0026lt;\u0026lt; b \u0026lt;\u0026lt; " ";\t\t\tcout \u0026lt;\u0026lt;endl;\t\t}\t\treturn;\t}\t\tfor(int k=1; k\u0026lt;=9 ; k++)\t{\t\tif(valid(b, k))\t\t{\t\t\tb=k;\t\t\tsolve(b);\t\t\tb=0;\t\t}\t}}int main() {\tint b;\tmemset(b, 0, sizeof(b));\tsolve(b);\treturn 0;}
■网友
from itertools import permutationsr=for a in permutations(range(1,10)): if a/(a*10+a)+a/(a*10+a)+a/(a*10+a)==1.0: r.append(a)\u0026#39;\u0026#39;\u0026#39;\u0026#39;\u0026#39;\u0026#39;暴力也很快,0.2s。结果是重复的,可见优化一下更好


    推荐阅读