-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcode_1.cpp
More file actions
48 lines (42 loc) · 1001 Bytes
/
code_1.cpp
File metadata and controls
48 lines (42 loc) · 1001 Bytes
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
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 15/04/19.
// Copyright © 2019 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
using namespace std;
int sol = INT_MAX;
bool isSolve( int x, int y, int m, int n) {
if( m > 0 && n > 0 && x <= m && y <= n) {
return true;
}
return false;
}
void helper( int x,int y, int m, int n, int counter ) {
if( isSolve(x, y, m, n) == true) {
if( x == m && y == n) {
if( sol > counter ) {
sol = counter;
}
}
helper(x + y, y , m, n , counter+ 1 );
helper(x, x + y , m, n , counter+ 1 );
}
}
int main() {
int m , n;
cout << "\nEnter M\t:\t";
cin >> m;
cout << "\nEnter N\t:\t";
cin >> n;
helper( 1 , 1 , m, n , 0);
if ( sol == INT_MAX) {
cout << "\nMinimum Number of Steps\t:\t-1\n";
}
else {
cout << "\nMinimum Number of Steps\t:\t" << sol << endl;
}
return 0;
}