-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe10.cpp
102 lines (86 loc) · 1.6 KB
/
e10.cpp
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
95
96
97
98
99
100
101
102
#include "../std_lib_facilities.h"
struct Input
{
int64_t a;
int64_t b;
};
istream &operator>> (istream &is, Input &input)
{
is >> input.a;
is >> input.b;
return is;
}
int64_t factorial (int64_t n);
int64_t permutations (Input input);
int64_t combinations (Input input);
Input &get_input (Input &input);
int main ()
{
while (true)
{
cout << "Do you want to know the permutations (p) or combinations (c) or (q) to quit?" << endl;
string s;
cin >> s;
Input input {};
switch (s.front())
{
case 'p':
{
input = get_input(input);
int64_t P = permutations(input);
cout << "P(" << input.a << "," << input.b << ") = " << P << endl;
break;
}
case 'c':
{
input = get_input(input);
int64_t C = combinations(input);
cout << "C(" << input.a << "," << input.b << ") = " << C << endl;
break;
}
case 'q':
{
exit(EXIT_SUCCESS);
}
default:
{
cerr << "Wrong input" << endl;
cin.clear();
fflush(stdin);
break;
}
}
}
}
int64_t permutations (Input input)
{
return factorial(input.a) / factorial((input.a - input.b));
}
int64_t combinations (Input input)
{
return permutations(input) / factorial(input.b);
}
int64_t factorial (int64_t n)
{
int64_t fact = 1;
if (n == 0)
{
return fact;
}
else
{
for (int i {1} ; i <= n ; ++i)
{
fact *= i;
}
}
return fact;
}
Input &get_input (Input &input)
{
cout << "Please enter possible numbers (a):" << endl;
cin >> input.a;
cout << "Please enter combinations (b):" << endl;
cin >> input.b;
return input;
}