-
Notifications
You must be signed in to change notification settings - Fork 7
/
StepJump.txt
94 lines (90 loc) · 1.63 KB
/
StepJump.txt
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
#include<iostream>
using namespace std;
int findCN(int n){
if(n==1)
return 1;
else if(n==2)
return 2;
else if(n==3)
return 4;
else
return findCN(n-1)+findCN(n-2)+findCN(n-3);
}
int findCMN(int m,int n){
if(m<1)
return 0;
if(n<m)
return 0;
if(n>3&&m==1)
return 0;
if(n<=3&&m==1)
return 1;
if(n==m)
return 1;
if(m==n-1)
return m;
return (findCMN(m-1,n-1)+findCMN(m-1,n-2)+findCMN(m-1,n-3));
}
int findCMNitr(int m,int n){
int a[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i<j) a[i][j]=0;
else if(i>2&&j==0) a[i][j]=0;
else if(i<=2&&j==0) a[i][j]=1;
else if(i==j) a[i][j]=1;
else if(j==i-1) a[i][j]=j+1;
else a[i][j]=a[i-1][j-1]+a[i-2][j-1]+a[i-3][j-1];
}
}
return a[n-1][m-1];
}
int main(){
//Part1
int n,m;
cout<<"Enter n:";
cin>>n;
int ans=findCN(n);
cout<<"Part1\nUsing recursion C(n)="<<ans<<endl;
//Part2
int a[n];
if(n==1)
cout<<"C(n)=1"<<endl;
else if(n==2)
cout<<"C(n)=2"<<endl;
else if(n==3)
cout<<"C(n)=4"<<endl;
else{
a[0]=1;a[1]=2;a[2]=4;
for(int i=3;i<n;i++){
int x=a[i-1];
int y=a[i-2];
int z=a[i-3];
a[i]=x+y+z;
}
cout<<"Part 2\nUsing itreation C(n)="<<a[n-1]<<endl;
}
//Part 3
int ans1=0;
for(int i=0;i<=n;i++){
ans1=ans1+findCMN(i,n);
}
cout<<"Part 3\nSum of all values="<<ans1<<endl;
//Part 4
ans=0;
for(int i=0;i<=n;i++){
ans=ans+findCMNitr(i,n);
}
cout<<"Part 4\nSum of all values="<<ans<<endl;
return 0;
}
Sample Output:
Enter n:13
Part1
Using recursion C(n)=1705
Part 2
Using itreation C(n)=1705
Part 3
Sum of all values=1705
Part 4
Sum of all values=1705