-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_1.cpp
34 lines (31 loc) · 797 Bytes
/
code_1.cpp
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
//
// main.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 31/07/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include<iostream>
using namespace std;
int cutRod(int *price, int n) {
if (n <= 0)
return 0;
int max_val = -1; //price can't be negative so using negative value initially to compare.
for (int i = 0; i < n; i++) {
max_val = max(max_val, price[i] + cutRod(price, n-i-1));
}
return max_val;
}
int main () {
int number_rod;
cout << "\nNumber of Rods\t:\t";
cin >> number_rod;
int array[number_rod];
cout << "\nEnter Price\n";
for( int i = 0; i < number_rod; ++i ) {
cin >> array[i];
}
cout <<"\nThe result is\t:\t" << cutRod(array , number_rod);
cout << endl;
return 0;
}