So, no one likes to carry around change (coins) and my question is, 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?You enter with no coins & you leave with no coins, and that is the least amount of change you have to bring. If you want to buy something, use a credit card ;)
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 :P)
- 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)
cheating :PSo, no one likes to carry around change (coins) and my question is, 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?You enter with no coins & you leave with no coins, and that is the least amount of change you have to bring. If you want to buy something, use a credit card ;)
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 :P)
- 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)
That's cheating.i say this is the same as my original answer event though i didnt explain all of it or understand the problem ;)
Let's break up in denomination of less than $1, 25¢, 10¢ and 5¢, respectively.
So to make anything less than $1, you'll need 3 quarters max.
Less than 25¢: 2 dimes max.
Less than 10¢: 1 nickel max.
Less than 5¢: 4 pennies max.
So 3 quarters, 2 dimes, 1 nickel and 4 pennies will make anything 1-99¢. The amount of coins when you enter will be at minimum and when you go out will also be at minimum since the cashier won't give you any change.
Or else you can carry one $1 coin (we have that in Canada, dunno in US) and the cashier will give minimum change.
The answer is 9 coins, assuming the object being bought is 1-99 centsErm, you might want to re-read the original post, as you answered a different least-change question :P
That 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 4P
Or 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?I'm not sure I get the question right or not (and all the rules).
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 :P)
- 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;
}
nitacku you need to reduce the amount entered and left with put together
#include <stdio.h>
typedef struct ChangeStruct
{
int pennies;
int nickels;
int dimes;
int quarters;
int amount;
} Change;
Change calculateChange(int amount)
{
int tempamount = amount;
Change result = {0,0,0,0,amount};
if (tempamount >= 25)
{
result.quarters = amount / 25;
tempamount -= result.quarters * 25;
}
if (tempamount >= 10)
{
result.dimes = tempamount / 10;
tempamount -= result.dimes * 10;
}
if (tempamount >= 5)
{
result.nickels = 1;
tempamount -= 5;
}
if (tempamount > 0)
{
result.pennies = tempamount;
}
return result;
}
int countCoins(Change counted)
{
int total;
total = counted.pennies + counted.nickels + counted.dimes + counted.quarters;
return total;
}
Change subtractChange(Change first, int second)
{
if (first.amount > second) return calculateChange(first.amount - second);
else return calculateChange(second - first.amount);
}
void printCoins(Change printNeeded)
{
printf("%d pennies\n%d nickels\n%d dimes\n%d quarters\n", printNeeded.pennies, printNeeded.nickels, printNeeded.dimes, printNeeded.quarters);
}
int main()
{
int smallestTotal, smallestE, currentE, smallestL, currentL, total;
Change smallestCoins, currentCoins, subtractCoins, returnCoins;
double smallestAverage=100.0, currentAverage;
for ( int i=0; i<100; i++)
{
total = 0;
currentCoins = calculateChange(i);
currentE = countCoins(currentCoins);
for ( int j=0; j<100; j++)
{
currentL = countCoins(subtractChange(currentCoins, j));
total += currentL + currentE;
}
currentAverage = total / 100.0;
if (currentAverage < smallestAverage)
{
smallestAverage = currentAverage;
smallestTotal = currentL + currentE;
smallestE = currentE;
smallestCoins = currentCoins;
}
}
printCoins(smallestCoins);
printf("With average %f\n", smallestAverage);
printf("Entered with %d coins\n", smallestE);
}
I believe the solution is to carry no change.
Whatever the cost of your purchase is and whatever coins you have, you can ensure you leave with the minimum amount of change possible by giving the vendor all of your change. Whatever coins properly contribute to the payment, the vendor will keep; the coins that do not will be returned to you. Since the cents amount of the purchase is an equally-distributed random number, subtracting your constant amount of change from this will result in another equally-distributed random number (mod 100). Whatever amount of change you contribute, the vendor will always have to return to you coins that sum to an equally-distributed random amount of cents from 0-99.
You cannot limit the amount of coins you end with, so the optimal solution is simply to limit the amount of coins you begin with.