-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutation_and_combination.cpp
More file actions
96 lines (76 loc) · 1.75 KB
/
permutation_and_combination.cpp
File metadata and controls
96 lines (76 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* =====================================================================================
*
* Filename: permutation_and_combination.c
*
* Description:
*
* Version: 1.0
* Created: 04/01/2017 17:08:21
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
//计算排列p (n, m) 即从n个数中取m个数并排列,共几种可能
int p(int n, int m) {
int64_t ret = 1;
int i;
if (m == 1)
return n;
printf("n %d m %d\n", n, m);
for (i = 1; i <= m; i ++) {
ret *= (n - i + 1);
}
return ret;
}
//计算组合c (n, m) 即从n个数中取m个数,共几种可能
int c(int n, int m) {
int64_t ret = 1;
int i = 1;
if (m == 0)
return 1;
if ((m == 1) || (m == n))
return n;
if (m > n / 2) {
m = n - m;
}
std::list<int> tmp(m);
std::list<int>::iterator it;
for (it = tmp.begin(); it != tmp.end(); it++) {
*it = i;
i ++;
}
printf("n %d m %d\n", n, m);
for (i = 1; i <= m; i ++) {
ret *= (n - i + 1);
if (tmp.empty())
continue;
for(it = tmp.begin(); it != tmp.end(); it++) {
if ((ret % *it)==0) {
ret /= *it;
printf("it %d\n", *it);
tmp.erase(it);
}
}
}
return ret;
}
int main() {
int n = 100;
int num_one = n;
int num_two = 0;
int i, j, k;
int ret = 0;
int tmp = n;
printf("p %d\n", p(6, 4));
printf("c %d\n", c(6, 4));
return 0L;
}