0 Members and 3 Guests are viewing this topic.
The answer is 9 coins, assuming the object being bought is 1-99 centsThat is because the thing requiring the most amount of coins is 94 cents which is done with the least ammount of coins which is 3Q 1D 1N 4POr 9 coins.
what coins should you enter a store with so that sum of the number of coins you enter a store and leave a store with is at a minimum?The general idea here is that you basically want to carry the least amount of change. So, the way this works, is you pick E number of coins to enter with, after buying your items (which have random number of cents) you leave with L number of coins. You want to minimize L + E.Rules: We're using American coins, so the choices are: penny - .01, nickel - .05, dime - .10, quarter - .25 (no half dollars, too rare ) The number of cents your purchase costs is random (So, a purchase would cost some dollar amount + [0 - 99] cents) You receive the minimum number of coins from the cashier ($.90 is not 9 dimes)Have at it. You'll of course have to provide some sort of justification for your answer.
#include <stdio.h> #include <stdlib.h>#include <string.h>#include <time.h>#include <math.h>#include "SFMT.c"typedef struct ChangeStruct{ int penny; int nickel; int dime; int quarter;} Change;Change min_change(int value);int main(){ Change carry; Change purchase; Change exchange; Change best_change; int transactions = 10000000; double best_result = 5; double average; int penny = 4; for (; penny >= 0; penny--) { int nickel = 1; for (; nickel >= 0; nickel--) { int dime = 2; for (; dime >= 0; dime--) { int quarter = 3; for (; quarter >= 0; quarter --) { int total_coins = 0; int x = 0; carry.penny = penny; carry.nickel = nickel; carry.dime = dime; carry.quarter = quarter; init_gen_rand(time(NULL)); for (; x<transactions; x++) { purchase = min_change(gen_rand32()%100); exchange.penny = abs(purchase.penny - carry.penny); exchange.nickel = abs(purchase.nickel - carry.nickel); exchange.dime = abs(purchase.dime - carry.dime); exchange.quarter = abs(purchase.quarter - carry.quarter); total_coins += exchange.penny + exchange.nickel + exchange.dime + exchange.quarter; } average = (double)total_coins/transactions; printf("Pennies: %d Nickels: %d Dimes: %d Quarters: %d\n", penny, nickel, dime, quarter); printf("Average coins per transaction: %f\n\n", average); if (average < best_result) { best_result = average; best_change = carry; } } } } } printf("Best Result: %f\n", best_result); printf("Pennies: %d Nickels: %d Dimes: %d Quarters: %d\n", best_change.penny, best_change.nickel, best_change.dime, best_change.quarter); return 0;}Change min_change(int value){ Change min_change; min_change.penny = 0; min_change.nickel = 0; min_change.dime = 0; min_change.quarter = 0; while (value) { if (value >= 25) { value -= 25; min_change.quarter++; } else { if (value >= 10) { value -= 10; min_change.dime++; } else { if (value >=5) { value -= 5; min_change.nickel++; } else { value -= 1; min_change.penny++; } } } } return min_change;}