Answer:
#include <stdio.h>
#include <stdlib.h>
int maxn(int a, int b);
int exchangeBlock(int m);
void printPartinion(int ptn[], int n);
int exchangePartinion(int ptn[], int n);
void generatePartinion(int n, int ptn[], int crsum, int cridx, int *mx);
int generate(int n);
int maxn(int a, int b) {
return (a > b) ? a : b;
}
int exchangeBlock(int m) {
return m / 4 + m / 3 + m / 2;
}
void printPartinion(int ptn[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ptn[i]);
printf("\n");
}
int exchangePartinion(int ptn[], int n)
{
int mx = 0;
int i = 0;
for (i = 0; i < n; i++)
mx += exchangeBlock(ptn[i]);
return mx;
}
void generatePartinion(int n, int ptn[], int crsum, int cridx, int *mx)
{
int num;
if (n == 0)
return;
/* If current sum is equal to n, then we found a partinion */
if (crsum == n) {
printPartinion(ptn, cridx);
*mx = maxn(exchangePartinion(ptn, cridx), *mx);
printf("__ %d\n", *mx);
}
/* Try placing all numbers from smallest to (n - current sum)
* at current index. The placed number must not be greater
* than previously placed numbers */
num = 1;
while (num <= n - crsum && (cridx == 0 || num <= ptn[cridx - 1])) {
ptn[cridx] = num;
generatePartinion(n, ptn, crsum + num, cridx + 1, mx);
num++;
}
}
int generate(int n)
{
int *ptn; /* array to store partinions */
int mx = 0; /* current largest currency */
ptn = (int *) malloc(n * sizeof(int));
generatePartinion(n, ptn, 0, 0, &mx);
return mx;
}
int main() {
int grams = 0;
int exch = 0;
int bl = 0;
printf("Input the value of the block in grams: ");
scanf("%d", &grams);
if (grams > 1000000000 || grams < 2)
return 1;
/* Answer 1: */
printf("\n");
exch = generate(grams);
printf("\n%d - %d\n", grams, exch);
printf("\nYou can get %d units of currency.\n", exch);
/* Answer 2: */
printf("\n\n");
for (bl = 12; bl > 0; bl--) {
exch = exchangeBlock(bl);
printf("%d - %d\n", bl, exch);
}
for (bl = 24; bl > 12; bl--) {
exch = exchangeBlock(bl);
printf("%d - %d\n", bl, exch - 13);
}
exch = (grams / 12) * 13 + exchangeBlock((grams + 12) % 12);
printf("\nYou can get %d units of currency.\n", exch);
return 0;
}
Comments
Leave a comment