add solution for lab 6
This commit is contained in:
27
labs/recursion/checkpoint1.cpp
Normal file
27
labs/recursion/checkpoint1.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int countPaths(int x, int y) {
|
||||
// if we are at the origin, there is 1 path
|
||||
if (x == 0 && y == 0) return 1;
|
||||
|
||||
int paths = 0;
|
||||
|
||||
// Move left if x > 0
|
||||
if (x > 0) paths += countPaths(x - 1, y);
|
||||
|
||||
// Move up if y > 0
|
||||
if (y > 0) paths += countPaths(x, y - 1);
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int start_x, start_y;
|
||||
cout << "Enter the starting coordinates (x, y): ";
|
||||
cin >> start_x >> start_y;
|
||||
|
||||
int result = countPaths(start_x, start_y);
|
||||
cout << "Number of paths to the origin: " << result << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user