Skip to content

Instantly share code, notes, and snippets.

@bojieli
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojieli/74265d9376041943c737 to your computer and use it in GitHub Desktop.
Save bojieli/74265d9376041943c737 to your computer and use it in GitHub Desktop.
C(m,n) and P(m,n)
#include<stdio.h>
void c_p_mn_array(int m, int n, int result[2]) {
int i;
result[1] = 1;
for (i=m-n+1; i<=m; i++)
result[1] *= i;
result[0] = result[1];
for (i=2; i<=n; i++)
result[0] /= i;
}
void c_p_mn_pointer(int m, int n, int *c, int *p) {
int i;
*p = 1;
for (i=m-n+1; i<=m; i++)
*p *= i;
*c = *p;
for (i=2; i<=n; i++)
*c /= i;
}
struct cp {
int c;
int p;
};
struct cp c_p_mn_struct(int m, int n) {
struct cp ans;
int i;
ans.p = 1;
for (i=m-n+1; i<=m; i++)
ans.p *= i;
ans.c = ans.p;
for (i=2; i<=n; i++)
ans.c /= i;
return ans;
}
int main() {
int m, n;
int result[2];
int combination, permutation;
struct cp ans;
scanf("%d%d", &m, &n);
c_p_mn_array(m, n, result);
printf("%d %d\n", result[0], result[1]);
c_p_mn_pointer(m, n, &combination, &permutation);
printf("%d %d\n", combination, permutation);
ans = c_p_mn_struct(m, n);
printf("%d %d\n", ans.c, ans.p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment