我用全排列写的,没有结果,应该是哪里错了
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define M 5
#define N 15
int a[N];
void init()
{
int i = 0;
while (a[i] = i+1, ++i < N);
}
int sub(int i, int j, int k)
{
return (a[i] + a[j] == a[k])
|| (a[i] + a[k] == a[j]);
}
int judge(int n)
{
switch(n)
{
case 2: case 4:
case 7: case 11: return 1;
case 3: return sub(0, 1, 2);
case 5: return sub(1, 3, 4);
case 6: return sub(2, 4, 5);
case 8: return sub(3, 6, 7);
case 9: return sub(4, 7, 8);
case 10: return sub(5, 8, 9);
case 12: return sub(6, 10, 11);
case 13: return sub(7, 11, 12);
case 14: return sub(8, 12, 13) && sub(9, 13, 14);
}
return 0;
}
void Output()
{
int i, j, k = 0;
for (i = 0;i < M;++i, puts(""))
for (j = 0;j <= i;++j)
printf("%3d", a[k++]);
printf("\n");
}
void fun(int ip)
{
int i, tmp;
if (ip >= N) return;
if (!judge(ip)) return;
if (ip == N-1) Output();
for(i = ip;i < N;++i)
{
tmp = a[i], a[i] = a[ip], a[ip] = tmp;
fun(ip+1);
tmp = a[i], a[i] = a[ip], a[ip] = tmp;
}
}
int main(void)
{
init();
a[0] = 1; a[1] = 4;
a[2] = 2; a[3] = 3;
fun(2);
a[0] = 2; a[1] = 4;
a[2] = 1; a[3] = 3;
fun(2);
a[0] = 3; a[1] = 4;
a[2] = 1; a[3] = 2;
fun(2);
return 0;
}
[
本帖最后由 azzbcc 于 2013-5-3 11:21 编辑 ]