1、
#include <stdio.h> int fac(int x) { if(x > 0) return x * fac(x - 1); else return 1; } int main(void) { int n, r; puts("please input two integers."); do { printf("n = "); scanf("%d", &n); if(n < 1) { puts("the range of n >= 1."); continue; } printf("r = "); scanf("%d", &r); if(r < 1) { puts("the range of r >= 1."); } if(n < r) { puts("n >= r."); } } while( n < r || n < 1 || r < 1); printf("result: %d ", fac(n) / (fac(r) * fac(n - r))); return 0; }
2、
#include <stdio.h> int fac(int x) { if(x > 0) return x * fac(x - 1); else return 1; } int com(int x, int y) { return fac(x) / (fac(y) * fac(x - y)); } int main(void) { int n, r; puts("please input two integers."); do { printf("n = "); scanf("%d", &n); if(n < 1) { puts("n >= 1"); continue; } printf("r = "); scanf("%d", &r); if(r < 1) { puts("r >= 1"); } if(n < r) puts("n >= r"); } while(n < r || n < 1 | r < 1); printf("result: %d ", com(n, r)); return 0; }
3、
#include <stdio.h> int com(int x, int y) { int tmp; if(x > 1 && y > 1) { tmp = com(x - 1, y - 1) + com(x - 1, y); } if(y == 0 || x == y) { tmp = 1; } if(y == 1) { tmp = x; } return tmp; } int main(void) { int a, b; puts("please input two integers."); do { printf("a = "); scanf("%d", &a); if(a < 1) { puts("a >= 1"); continue; } printf("b = "); scanf("%d", &b); if(b < 1) { puts("b >= 1"); } if(a < b) puts("a >= b"); } while(a < b || a < 1 | b < 1); printf("result: %d ", com(a, b)); return 0; }