Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by moh05 · May 19, 2016 at 11:20 AM · c#algorithm

Find the least number of bills required to pay for a certain amount

Find the least number of bills required to pay for a certain amount down vote favorite I am working on a game and I need a payment algorithm for the following scenario:

1- Suppose the money bills in the game are: 10,5,4,3,2,1

2- AI needs to choose the least number of bills needed to cover the exact amount,i.e if the required to pay is 8 and AI has (4,4,3,3,2)...He can choose (4,4) but not (3,3,2)

3- In case AI can't make the exact amount using the bills he has, he should choose the combination such that it gives the amount with the least difference value, i.e. if the required amount to pay is 7 and the AI has the following bills ( 10,5,4,4), he choses (4,4) which gives the player 1 more above the needed amount.

I am stuck in implementing this in C#. Any ideas on how to solve this problem?

Here is the code:

 public void PreparePayment(int neededAmount)
     {
 
         int remainingAmount = neededAmount;
 
         int chosenAmount;
 
         while (remainingAmount > 0) 
         {
             chosenAmount = 0;
 
             foreach (int moneyValue in sortedValues )            
             {
                 if (moneyValue <= remainingAmount) 
                 {                                                        chosenCardsToPay.Add (moneyValue); //Add Bill Value to my candidate list
                     remainingAmount = remainingAmount - moneyValue;
                     chosenAmount = moneyValue;
                     break;
                 }    
             }
             if (chosenAmount != 0)
                 sortedValues.Remove (moneyValue);//Remove Chosen Bill from Initial List
             else //If all bill values are greater than remaining amount, i choose the bill with smallest value and add to the candidate list
             {
                 chosenAmount = sortedValues.Last();
                 sortedValues.Remove(chosenAmount);                            chosenCardsToPay.Add (chosenAmount);
                 remainingAmount = remainingAmount - moneyValue;
             }
         }
     }

Thank you!⁠

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · May 19, 2016 at 11:47 AM 1
Share

( "Find the least number of bills required to pay for a certain amount down vote favorite" sounds an awful lot like you copied this question from another website? )

The parameters of your problem seem pretty well prescribed. What have you tried? Where did you get stuck?

avatar image moh05 tanoshimi · May 19, 2016 at 02:07 PM -1
Share

Check above, thanks

avatar image moh05 tanoshimi · May 21, 2016 at 07:43 AM 0
Share

Any help here ?:)

avatar image meat5000 ♦ · May 21, 2016 at 08:04 AM 0
Share

Probably from SO

http://webcache.googleusercontent.com/search?q=cache:$$anonymous$$cqGjc1lXXwJ:stackoverflow.com/questions/37321590/find-the-least-number-of-bills-required-to-pay-for-a-certain-amount+&cd=2&hl=en&ct=clnk≷=uk&client=ubuntu

Curiously, this is the cached version and clicking the actual Google link takes you here

http://stackoverflow.com/questions/4247662/the-$$anonymous$$imum-number-of-coins-the-sum-of-which-is-s

avatar image moh05 meat5000 ♦ · May 22, 2016 at 07:52 AM 0
Share

I wrote it there...do you have any tips? :)

avatar image Bunny83 meat5000 ♦ · May 25, 2016 at 12:25 AM 0
Share

His question has been marked as a duplicate and has a reference to the other question.

http://stackoverflow.com/questions/37321590/find-the-least-number-of-bills-required-to-pay-for-a-certain-amount

Since SO is actually a more general program$$anonymous$$g Q&A site i don't think this question belongs here on UA as it has nothing to do with Unity. Also the referenced question seems to have answers.

Apart from that case 1 is quite trivial as you just have to keep subtracting the next smaller note as long as the remaining amount is larger or equal to the current note. Case 2 is more complicated as i don't see a way around testing all combinations that cover the target amount and pick the best one. This should be quite easy with a recursive method and a struct that represents the current money state of each "bill type".

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Could someone explain how this could be done? 0 Answers

Improving Grid Algorithm 1 Answer

How to make a Maze with multiple paths and no dead ends? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges