图片附件: 游客没有浏览图片的权限,请
登录 或
注册
#include <stdio.h>
#include <stdlib.h>
void _swap(char *c1, char *c2)
{
*c1 ^= *c2;
*c2 ^= *c1;
*c1 ^= *c2;
}
void _print(int *a, char *c, int n, int mul)
{
int i, sum=0;
for (i=0; i<n; i++)
if (c[i] == 1)
sum += a[i];
if (sum%mul==0)
{
for (i=0; i<n; i++)
if (c[i] == 1)
printf("%d + ", a[i]);
printf("\b\b \b");
printf("= %d
(%d*%d)\n", sum, sum/mul, n);
}
}
void _combine(int *a, int n, int m, int mul)
{
int i, j, k;
char *c=(char *)calloc(n, sizeof(char));
for (i=0; i<m; i++)
c[i] = 1;
while (1)
{
_print(a, c, n, mul);
for (i=0; i<n-1; i++)
if (c[i]==1 && c[i+1]==0)
break;
if (i==n-1)
break;
_swap(&c[i], &c[i+1]);
for (j=0; j<i && c[j]==1; j++) NULL;
for (k=j,j++; j<i; j++)
if (c[j] == 1)
_swap(&c[k++], &c[j]);
}
free(c);
}
main()
{
int a[]={2,5,6,3,18,7,11,19};
int n=sizeof(a)/sizeof(int);
int i;
for (i=0; i<n; i++)
printf("%d ", a[i]);
printf("\nN = %d\n", n);
for (i=0; i<n; i++)
_combine(a, n, i+1, n);
}