可是如果只遍历的话,那也太繁琐了,要对每个方框都遍历啊
认认真真的学习,踏踏实实的走路:戒骄戒躁!!!
#include <stdio.h> void Swap(int* p1, int* p2) { int tmp = *p1; *p1 = *p2; *p2 = tmp; } void Reverse(int* start, int* end) { while (start < end) Swap(start++, --end); } int NextPermutation(int* start, int* end) { int* p, *sp = end - 2; if (start >= end - 1) return 0; while (sp >= start) { if (sp[0] < sp[1]) break; --sp; } if (sp < start) { Reverse(start, end); return 0; } for (p = end - 1; p > sp; --p) if (*p > *sp) { Swap(sp, p); break; } Reverse(sp + 1, end); return 1; } /* * X1/(X2*10+X3) + X4/(X5*10+X6) + X7/(X8*10+X9) = 1 *<==> * X1*(X5*10+X6)*(X8*10+X9) + X4*(X2*10+X3)*(X8*10+X9) + X7*(X2*10+X3)*(X5*10+X6) = (X2*10+X3)*(X5*10+X6)*(X8*10+X9) */ int main() { int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; do { if (x[0] * (x[4] * 10 + x[5]) * (x[7] * 10 + x[8]) + x[3] * (x[1] * 10 + x[2]) * (x[7] * 10 + x[8]) + x[6] * (x[1] * 10 + x[2]) * (x[4] * 10 + x[5]) == (x[1] * 10 + x[2]) * (x[4] * 10 + x[5]) * (x[7] * 10 + x[8])) { printf("%4d%10d%10d\n", x[0], x[3], x[6]); printf("------- + ------- + ------- = 1\n"); printf("%4d%10d%10d\n\n", x[1] * 10 + x[2], x[4] * 10 + x[5], x[7] * 10 + x[8]); } } while (NextPermutation(x, x + 9)); return 0; }