#include #include #include #include int genparams ( int n, int *c, int *s, int *t, int *r, double *p, double *q ) { int C; int i; C = 0; for (i=0; i best) { best = curr; count = k; } } B[i][j] = count; T[i][j] = best; } } j = C; for (i=n-1; i>=0; --i) { m[i] = B[i][j]; j -= m[i] * c[i]; } printf("\n+++ Part 1: Best buying option\n"); j = 0; for (i=0; i T[0][j]) { T[0][j] = best; U[0][j] = 1; } } for (i=1; i best) { best = curr; count = k; maintain = 0; } q2 *= q[i]; curr = T[i-1][j-k*c[i]] + q2*t[i] + (1-q2)*s[i] - k*r[i]; if (curr > best) { best = curr; count = k; maintain = 1; } } B[i][j] = count; T[i][j] = best; U[i][j] = maintain; } } j = C; for (i=n-1; i>=0; --i) { m[i] = B[i][j]; u[i] = U[i][j]; j -= m[i] * c[i]; } printf("\n+++ Part 2: Best buying option\n"); j = 0; for (i=0; i 1) n = atoi(argv[1]); else scanf("%d", &n); printf("n : %d\n", n); c = (int *)malloc(n * sizeof(int)); s = (int *)malloc(n * sizeof(int)); t = (int *)malloc(n * sizeof(int)); r = (int *)malloc(n * sizeof(int)); p = (double *)malloc(n * sizeof(double)); q = (double *)malloc(n * sizeof(double)); C = genparams(n,c,s,t,r,p,q); optimalbuy1(n,C,c,s,t,p); optimalbuy2(n,C,c,s,t,r,p,q); exit(0); }