Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
23 May, 2013, 10:07:57 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: 1 2 [3]   Go Down
  Print  
Author Topic: Least amount of change -  (Read 783 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
nitacku
LV6 Super Member (Next: 500)
******
Offline Offline

Gender: Male
Last Login: 05 April, 2013, 06:19:18
Date Registered: 27 August, 2008, 06:52:09
Posts: 315


Total Post Ratings: +29

View Profile
« Reply #30 on: 27 June, 2012, 03:13:20 »
0

nitacku you need to reduce the amount entered and left with put together

I think the solution in that case is to bring nothing.

The reason: There is never 100% probability that you eliminate what you brought. Even if it was 100% probable that you would use what you brought, it would simply cancel itself. For example: If you bring one penny, 80% of the time it is used. This means that your average number of coins is 1 + (4.7 - 0.8). You see? 1 - 0.8 > 0, which means bringing anything defeats the purpose.
« Last Edit: 27 June, 2012, 03:13:54 by nitacku » Logged
ruler501
Crazy Freshman
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: Today at 06:39:48
Date Registered: 08 November, 2010, 02:32:33
Location: In a cave with two spots of light and lots of meat
Posts: 2382


Total Post Ratings: +49

View Profile
« Reply #31 on: 27 June, 2012, 04:12:01 »
0

I brute forced it too to see if that might show something other then the bring nothing

heres the code it runs with the standard C lib

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#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);
}

and the result was
0 pennies
0 nickels
0 dimes
0 quarters
With average 4.700000
Entered with 0 coins

This seems wrong to me so please help me find if there is an error in my code

EDIT: I fixed it so that 0 coins was an option
« Last Edit: 27 June, 2012, 04:28:51 by ruler501 » Logged


Spoiler for "Projects":
My current games I am working on our:
  I might have an improved C version of this somewhere...
pSDL too lazy too make a userbar so I'll just link to the topic i update routinely http://www.omnimaga.org/index.php?board=146.0
Spoiler for "Misc images of test things":
NerdTests.com says I'm a Dorky Nerd God.  Click here to take the Nerd Test, get geeky images and jokes, and talk to others on the nerd forum!My computer geek score is greater than 100% of all people in the world! How do you compare? Click here to find out!"<br />[url=http://www.nerdtests.com/ft_personality.php?ref=42769
[/url]
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

------END GEEK CODE BLOCK------
"KnifeOn!  Apply directly to the forehead!  KnifeOn is available without a prescription at retailers nationwide."
thepenguin77
z80 Assembly Master
LV10 31337 u53r (Next: 2000)
**********
Offline Offline

Gender: Male
Last Login: Today at 01:15:58
Date Registered: 14 December, 2009, 04:21:52
Location: Purdue
Posts: 1484


Topic starter
Total Post Ratings: +778

View Profile
« Reply #32 on: 28 June, 2012, 01:30:22 »
0

Well, it looks like runer had it right from the start. Carrying 0 coins really is the best option.

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.

And with 2 brute force checks to back this, I'm happy. It looks like I've been doing it right all along. (Nitaku said his results were wrong and I think "someone" solved the wrong problem.
Logged

zStart v1.3.011 4-29-2013  zStart fully works on 83+BE's (except custom font)
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112
Pages: 1 2 [3]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.255 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.