-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_2.cpp
38 lines (34 loc) · 793 Bytes
/
code_2.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
35
36
37
38
//
// 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) {
int val[n+1];
val[0] = 0;
int i, j;
for (i = 1; i<=n; i++) {
int max_val = -1;
for (j = 0; j < i; j++)
max_val = max(max_val, price[j] + val[i-j-1]);
val[i] = max_val;
}
return val[n];
}
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;
}