Author Topic: Programming Challenge N  (Read 3405 times)

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Programming Challenge N
« on: 13 September 2006, 10:16 »
Okay, this one's a bit easier than usual.

Find the minimal number of coins you'd need to get n cents (assume dollar, five dollar, etc. coins don't exist).

For example, if n = 19, the answer is 6 (a nickle, a dime, and four pennies).

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Programming Challenge N
« Reply #1 on: 13 September 2006, 11:31 »
Wow, that is a hell of a lot easier than your other challenges.  I wrote a program to do this using C++ when I was in college - using Borland Turbo C++ for MS-DOS (this was when Windows 95 was not yet widespread).

H_TeXMeX_H

  • Member
  • **
  • Posts: 1,988
  • Kudos: 494
    • http://draconishinobi.50webs.com/
Re: Programming Challenge N
« Reply #2 on: 13 September 2006, 21:17 »
Perhaps I'll actually attempt this one. Don't have time now tho ... maybe later today.

solemnwarning

  • Member
  • **
  • Posts: 747
  • Kudos: 338
    • http://www.solemnwarning.net
Re: Programming Challenge N
« Reply #3 on: 14 September 2006, 09:06 »
Code: [Select]
#define n 19

int count = 0;
int count_1 = 0;
int count_2 = 0;
int count_5 = 0;
int count_10 = 0;
int count_20 = 0;
int count_50 = 0;
while(count < n) {
if(count + 50 <= n) {
count += 50;
count_50++;
}else if(count + 20 <= n) {
count += 20;
count_20++;
}else if(count + 10 <= n) {
count += 10;
count_10++;
}else if(count + 5 <= n) {
count += 5;
count_5++;
}else if(count + 2 <= n) {
count += 2;
count_2++;
}else if(count + 1 <= n) {
count++;
count_1++;
}
}


Not tested but it should work :)

(I used UK coins below
-----BEGIN GEEK CODE BLOCK-----
 Version: 3.1
 GCS/CM 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------

H_TeXMeX_H

  • Member
  • **
  • Posts: 1,988
  • Kudos: 494
    • http://draconishinobi.50webs.com/
Re: Programming Challenge N
« Reply #4 on: 14 September 2006, 23:13 »
Here it is in shell script (just for fun):

Code: [Select]
#!/bin/sh

printf "Enter the number of cents ==> "
read number

coins=0

for i in 50 25 10 5 1
do
 while test "$number" -ge "$i"
 do
  let coins++
  let number-=$i
 done
done

echo "The answer is $coins coins"

exit 0

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: Programming Challenge N
« Reply #5 on: 15 September 2006, 04:32 »
This approach works. However, I challenge you to come up with something more compact!

H_TeXMeX_H

  • Member
  • **
  • Posts: 1,988
  • Kudos: 494
    • http://draconishinobi.50webs.com/
Re: Programming Challenge N
« Reply #6 on: 15 September 2006, 05:40 »
I don't think it'll get any more compact than that :( ... anyone else wanna try ?

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Programming Challenge N
« Reply #7 on: 15 September 2006, 09:38 »
I'm a pretty shitty programmer, I can barely slap together a helloworld anymore without a myriad of syntax errors.  But hey, I got this one to work!  For some reason, I got it in my head that integer division was the way to go with this.  I wanted to use perl, just because I'm trying to learn it right now, but perl only does float division.  So does JavaScript.  So I had to pull out a couple C manuals.

 
Code: [Select]
#include

main()
{

int value;
int i;
int total=0;
int coins[5]={50,25,10,5,1};

printf("Enter an integer monetary value: \n");
scanf("%d", &value);

for(i=0; i<5; i++) {
total += value / coins[i];
value = value % coins[i];
}

printf("That's %d coins \n", total);
}
Pretty compact, I guess - the actual meat of the program is only 2 lines long.  Keepin' it real - straight-up old skool K&R brace style.

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: Programming Challenge N
« Reply #8 on: 16 September 2006, 05:55 »
That's essentially what I was thinking about. You can do all this without a loop, however! I challenge youse guys.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Programming Challenge N
« Reply #9 on: 16 September 2006, 06:04 »
Unless you have some shifty if/else ladder in mind, I'm not sure what you're looking for.  Maybe a hint?

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: Programming Challenge N
« Reply #10 on: 16 September 2006, 06:45 »
The operators / and %.

sledz41

  • Newbie
  • *
  • Posts: 12
  • Kudos: 10
Re: Programming Challenge N
« Reply #11 on: 16 September 2006, 07:26 »
Therefore the code would be:
Code: [Select]
#include
 
int main ()
{
 
int value;
int count = 0;
 
printf("Enter an integer monetary value: \n");
scanf("%d", &value);
 
count += value / 50;
value %= 50;
 
count += value / 25;
value %= 25;
 
count += value / 10;
value %= 10;
 
count += value / 5;
value %= 5;
 
count += value / 1;
value %= 1;
 
printf("Minimum number of coins required is: %d\n", count);
 
return 0;
}
?

Linux Is freedom of choice

Microsoft is a cage of ignorance
 :)  :tux:                               :tux:  :)

TheQuirk

  • VIP
  • Member
  • ***
  • Posts: 2,154
  • Kudos: 315
Re: Programming Challenge N
« Reply #12 on: 16 September 2006, 08:16 »
Yup! Good job.

Now, would anyone be interested in me breaking up the last challenge into multiple parts with hints?

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Programming Challenge N
« Reply #13 on: 16 September 2006, 09:36 »
Quote from: sledz41
Therefore the code would be:
Code: [Select]
program
?

Well, that works, I guess.  But I don't think it is good style.  To switch to a different currency system, such as Britain, who uses 2pence coins, would be very difficult.  Adding bills to the concept is also problematic, since you would have to at least double the amount of code.  My version, using a single array to contain all the possible values, is much more easily converted - no matter what kind of monetary system you want to implement, you only have to change one line.

Although I realize that this was an exercise in thinking, maintainability and portability are almost as important as algorithm design, these days.  Good computer programming is a marriage of design and implementation.

worker201

  • Global Moderator
  • Member
  • ***
  • Posts: 2,810
  • Kudos: 703
    • http://www.triple-bypass.net
Re: Programming Challenge N
« Reply #14 on: 16 September 2006, 09:38 »
Quote from: TheQuirk
Yup! Good job.

Now, would anyone be interested in me breaking up the last challenge into multiple parts with hints?
You mean this one?
http://www.microsuck.com/forums/showthread.php?t=10050

Sure, go ahead.