-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice_combinations.cpp
More file actions
76 lines (70 loc) · 1.24 KB
/
dice_combinations.cpp
File metadata and controls
76 lines (70 loc) · 1.24 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
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
int diceCombTDDP(int n,int *dp){
if (n == 0) return 1;
if(n < 0) return 0;
if(dp[n] > 0){
return dp[n];
}
int ans = 1;
for(int i=0; i <n ;i++){
for(int k = 1;k<=6;k++){
if(k > i) break;
ans += diceCombTDDP(i-k,dp);
}
}
return dp[n] = ans;
}
int diceCombBUDP(int n){
int *dp = new int[n+1];
dp[0] = 1;
for(int i = 1;i<=n;i++){
dp[i] = 0;
for(int k=1;k<=6;k++){
if(k > i) break;
dp[i] += dp[i-k];
}
}
int output = dp[n];
delete [] dp;
return output;
}
// recursive
int diceComb(int n){
if (n == 0) return 1;
if(n < 0) return 0;
int ans = 1;
for(int i=0; i <n ;i++){
for(int k = 1;k<=6;k++){
if(k > i) break;
ans += diceComb(i-k);
}
}
return ans;
}
// better recursive sol
int diceCombRec(int n){
if (n == 0) return 1;
if(n < 0) return 0;
int res = 0;
for(int i=1;i<=6;i++){
res += diceCombRec(n-i);
}
return res;
}
int main(){
int n;
cin>>n;
int *dp = new int[n+1];
for (int i = 0; i <= n; ++i)
{
dp[i] = 0;
}
cout<<diceComb(n)<<endl;
cout<<diceCombTDDP(n,dp)<<endl;
cout<<diceCombBUDP(n)<<endl;
cout<<diceCombRec(n);
delete [] dp;
}