只要求高手把算法写出来,谢谢了
俺沒做出來,有個寫了一半的程式,灌回水:
#include"stdio.h"
#include"math.h"
#define UBOUND 20
int isOK(int intNum)
{
int i,j=0;
for(i=2;i<sqrt(intNum)+1;++i)
{
if(intNum%i==0)
{
j=1;break;
}
}
if(j==0)return(1);
else return(0);
}
int over(int ary[])
{
int i,j=0;
for(i=0;i<UBOUND-1;++i)
if(isOK(ary[i]+ary[i+1])!=1)
{
j=1;break;
}
if(j==0)return(1);
else return(0);
}
int main()
{
struct data
{
int val;
int flag;
}ary[2][UBOUND];
int i,j,k=0,tmp[20];
for(i=0;i<UBOUND;i++)
{
ary[0][i].val=i+1;
ary[0][i].flag=0;
}
do
{
for(i=0;i<UBOUND-1;i++)
{
for(j=i+1;j<UBOUND;j++)
{
if(ary[0][i].flag==0 && ary[0][j].flag==0)
if(isOK(ary[0][i].val+ary[0][j].val)==1)
{
ary[0][i].flag=1;ary[0][j].flag=1;
tmp[k++]=ary[0][i].val;
tmp[k++]=ary[0][j].val;
}
}
}
k=0;
for(i=0;i<UBOUND;i++)
{
ary[0][i].val=tmp[i];
}
for(i=0;i<UBOUND;++i)
printf("%3d",tmp[i]);
getchar();
}while (over(tmp)==0);
for(i=0;i<UBOUND;++i)
printf("%3d",tmp[i]);
}
死循環了!!
#include <stdio.h>
int prim[]={2,3,5,7,11,13,17,19,23,29,31,37};
int b[21];
int counter=0;
int a[20];
int k=1;
void function(int);
int main()
{
int i;
for (i=1;i<21;i++)
b[i]=0;
a[0]=20;
function(20);
getch();
return 0;
}
void function(int j)
{
int m,i;
b[j]=1;
counter++;
if(counter==20)
{
if(j==3||j==9||j==11||j==17)
{
for (m=0;m<k;m++)
printf("%4d",a[m]);
printf("\n");
getch();
}
}
else
{
for(i=0;i<12;i++)
if(b[prim[i]-j]==1||prim[i]-j<=0||prim[i]-j>=20)
continue;
else
{
a[k++]=prim[i]-j;
function(prim[i]-j);
k--;
counter--;
b[prim[i]-j]=0;
}
}
}