回复 楼主 雪花神剑
#include"stdio.h"main()
{int a[10],n,t,i,p=1,sum;
a[10]={0,1,2,3,4,5,6,7,8,9};
printf("input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ t=10-i;
p=p*t
}
sum=p/2;
printf("%d",sum);
}
这是一个组合分配问题,只要解决算法问题就简单了。
#include<stdio.h> int b[10]; int count=0; void solve(int a[],int curr,int index,int n) { if(curr==n) { for(int i=0;i<n;i++) { printf("%d ",b[i]); } count++; puts(""); } else { for(int i=index;i<10;i++,index++) { b[curr]=a[index]; solve(a,curr+1,index+1,n); } } } int main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; int n; scanf("%d",&n); solve(a,0,0,n); printf("count=%d\n",count); return 0; }
#include <stdio.h> #include <stdlib.h> void printc(int *comb, int k) { printf("{"); int i; for (i = 0; i < k; i++) printf("%d, ", comb[i] + 1); printf("\b\b}\n"); } int next_comb(int *comb, int k, int n) { int i = k - 1; comb[i]++; while ((i > 0) && (comb[i] >= n - k + 1 + i)) { i--; comb[i]++; } if (comb[0] > n - k) return 0; for (i = i + 1; i < k; i++) comb[i] = comb[i - 1] + 1; return 1; } int main() { int n = 10; /* The size of the set; */ int k; int comb[10]; /* comb[i] is the index of the i-th element in the combination */ /* Setup comb for the initial combination */ int i; printf("Put the k value:"); scanf("%d",&k); for (i = 0; i < k; i++) comb[i] = i; /* Print the first combination */ printc(comb, k); /* Generate and print all the other combinations */ while (next_comb(comb, k, n)) printc(comb, k); return 0; }