forked from WaderManasi/Knowing-DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WaderManasi#7 from Umang2002/master
Recursion types
- Loading branch information
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
using namespace std; | ||
|
||
void fun1(int n){ | ||
|
||
if(n > 0){ | ||
|
||
fun1(n-1); | ||
cout << n <<endl; | ||
|
||
} | ||
} | ||
|
||
int main(){ | ||
int x; | ||
cin >> x; | ||
fun1(x); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
using namespace std; | ||
void fun2(int); | ||
void fun1(int n){ | ||
if(n > 0){ | ||
cout << n <<endl; | ||
fun2(n-1); | ||
} | ||
} | ||
void fun2(int n){ | ||
if(n > 1){ | ||
cout << n <<endl; | ||
fun1(n/2); | ||
} | ||
} | ||
int main(){ | ||
int x; | ||
cin >> x; | ||
fun1(x); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
|
||
#include <iomanip> | ||
|
||
using namespace std; | ||
|
||
const int D =8; | ||
|
||
bool canPlace(int board[D][D],int n,int r,int c){ | ||
return | ||
r >= 0 && r <n && | ||
c >=0 && c < n && | ||
board[r][c]==0; | ||
} | ||
bool solveKnight(int board[D][D],int n,int move_no,int curRow,int curCol){ | ||
if(move_no == n*n){ | ||
return true; | ||
} | ||
int row_dir[]= {+2,+1,-1,-2,-2,-1,+1,+2}; | ||
int col_dir[]= {+1,+2,+2,+1,-1,-2,-2,-1}; | ||
for(int cur_dir =0;cur_dir<8;cur_dir++){ | ||
int nextRow = curRow + row_dir[cur_dir]; | ||
int nextCol = curCol + col_dir[cur_dir]; | ||
if(canPlace(board,n,nextRow,nextCol) == true){ | ||
board[nextRow][nextCol]= move_no + 1; | ||
bool isSucess= solveKnight(board,n,move_no+1,nextRow,nextCol); | ||
if(isSucess ==true){ | ||
return true; | ||
}else { | ||
board[nextRow][nextCol]=0; | ||
|
||
} | ||
} | ||
} | ||
return false; | ||
} | ||
void printBoard(int board[D][D],int n){ | ||
for(int i=0;i<n;i++){ | ||
for(int j=0;j <n;j++){ | ||
cout << setw(3) << board[i][j] << " "; | ||
} | ||
cout << endl; | ||
} | ||
} | ||
|
||
int main(){ | ||
int board[D][D] = {0}; | ||
int n; | ||
cin >>n; | ||
|
||
board[0][0]=1; | ||
bool ans = solveKnight(board,n,1,0,0); | ||
if(ans == true ){ | ||
|
||
printBoard(board,n); | ||
}else { | ||
cout << "sorry!!!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
using namespace std; | ||
int fun1(int n){ | ||
if(n > 100){ | ||
return n-10; | ||
}else{ | ||
return fun1(fun1(n+11)); | ||
}} | ||
int main(){ | ||
int x; | ||
int y; | ||
cin >> x; | ||
y =fun1(x); | ||
cout << y <<endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
using namespace std; | ||
|
||
void fun1(int n){ | ||
|
||
if(n > 0){ | ||
cout << n <<endl; | ||
fun1(n-1); | ||
}} | ||
int main(){ | ||
int x; | ||
cin >> x; | ||
fun1(x); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
using namespace std; | ||
void fun1(int n){ | ||
if(n > 0){ | ||
cout << n <<endl; | ||
fun1(n-1); | ||
fun1(n-1); | ||
} | ||
} | ||
int main(){ | ||
int x; | ||
cin >> x; | ||
fun1(x); | ||
return 0; | ||
} |