• 主页
• 问答

# 求组合算法

sunboyqq23 2010-07-23 07:10:19

...全文
35 点赞 收藏 6

6 条回复

Six_dimensional 2010-07-24

``````
#include <stdio.h>
#define  Limit 43

int lent[5]={20,15,8,7,5};
int current;
int count;

void Slove(int Num,int *arr)
{
int i;
int *p=arr;
if(Num==0)
{
for (i=lent[Num]-1;i>=0;i--)
{
if(current+p[i]<=Limit)
{
count+=i+1;
return ;
}
}
return ;
}
else
{
for (i=lent[Num]-1;i>=0;i--)
{
if(Num==4)
current=0;
current+=p[i];
if(current<Limit)
Slove(Num-1,p+lent[Num]);
current-=p[i];
}
}
}

int main()
{
int arr1[20]={5,9,36,39,42,49,57,59,61,65,67,69,72,75,76,78,81,83,85,89};
int arr2[15]={12,15,19,24,29,34,36,39,42,49,52,57,59,61,65};
int arr3[8]={23,24,41,50,55,57,60,65};
int arr4[7]={1,5,9,36,39,42,49};
int arr5[5]={1,5,9,31,54};
Slove(4,arr5);
printf("%d", count);
return 0;
}
``````

sunboyqq23 2010-07-24
[Quote=引用 4 楼 qiyu1988 的回复:]
C/C++ code

#include<stdio.h>
#include<stdlib.h>
#define M 50

int main()
{
int arr1[10]={1,2,6,14,35,45,46,89,100,101};
int arr2[5]={0,3,8,9,11};
int arr3[8]={1,3,4,10,11,1……
[/Quote]

qiyu1988 2010-07-23
``````
#include<stdio.h>
#include<stdlib.h>
#define   M   50

int main()
{
int arr1[10]={1,2,6,14,35,45,46,89,100,101};
int arr2[5]={0,3,8,9,11};
int arr3[8]={1,3,4,10,11,13,22,40};

int i,j,k;

for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<8;k++)
{
if(arr1[i]+arr2[j]+arr3[k]>M)
break;
else
printf("%d %d %d\n",arr1[i],arr2[j],arr3[k]);

}
if(arr1[i]+arr2[j]+arr3[0]>M)
break;
}
if(arr1[i]+arr2[0]+arr3[0]>M)
break;
}
return 0;
}
``````

qiyu1988 2010-07-23

#include<stdio.h>
#include<stdlib.h>
#define M 50

int main()
{
int arr1[10]={1,2,6,14,35,45,46,89,100,101};
int arr2[5]={0,3,8,9,11};
int arr3[8]={1,3,4,10,11,13,22,40};

int i,j,k;

for(i=0;i<10;i++)
{
for(j=0;j<5;j++)
{
for(k=0;k<8;k++)
{
if(arr1[i]+arr2[j]+arr3[k]>M)
break;
else
printf("%d %d %d\n",arr1[i],arr2[j],arr3[k]);

}
if(arr1[i]+arr2[j]+arr3[0]>M)
break;
}
if(arr1[i]+arr2[0]+arr3[0]>M)
break;
}
return 0;
}

Six_dimensional 2010-07-23

chj_2003 2010-07-23

3.2w+