-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmin_op(nf).c
More file actions
45 lines (36 loc) · 720 Bytes
/
min_op(nf).c
File metadata and controls
45 lines (36 loc) · 720 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
//DP: Minimum Operations
#include <stdio.h>
#include <stdlib.h>
int memo[100];
int min( int x, int y ) {
int min = x;
if( y < x )
min = y;
return min;
}
int min_oper( int n ) {
int r;
if( n == 0 )
return 0;
if( memo[n] != -1 )
return memo[n];
r = 1 + min_oper( n-1 );
if( n % 2 == 0 )
r = min( r, 1+min_oper( n/2 ) );
memo[n] = r;
return r;
}
int main( void ) {
int t, n, i, j;
printf("\nEnter the value of t:");
scanf("%d",&t);
for( i = 0; i < t; i++ ) {
printf("\nEnter the value of n:");
scanf("%d",&n);
for( j = 0; j < 100; j++ ) //Initialization
memo[j] = -1;
int ans = min_oper( n );
printf("\nThe minimum number of operations required are: %d\n", ans);
}
return 0;
}