Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. int[] billValues = { 1, 5, 10, 20, 50, 100 };
  4. int[] myBills = new int[6]; // which bills I have
  5. int[] paidBills = new int[6]; // what bills I paid
  6. int[] changeBills = new int[6]; // what bills I got as change
  7.  
  8. int amountToPay = 150;
  9.  
  10. int amountLeftToPay = amountToPay;
  11.  
  12. myBills[0] = 0; //1
  13. myBills[1] = 0; //5
  14. myBills[2] = 1; //10
  15. myBills[3] = 0; //20
  16. myBills[4] = 0; //50
  17. myBills[5] = 2; //100
  18.  
  19. int Total = (myBills[0] * 1) + (myBills[1] * 5) + (myBills[2] * 10) + (myBills[3] * 20) + (myBills[4] * 50) + (myBills[5] * 100);
  20.  
  21. if(amountToPay > Total)
  22. {
  23. Console.WriteLine("Not enough money");
  24. Console.ReadLine();
  25. return;
  26. }
  27.  
  28. for (int i = billValues.Length - 1; i >= 0; i--) // biggest FIRST
  29. {
  30. // basically keep paying with e.g. hundreds as long as the amount
  31. // left is 100+, etc, then go on to 50's etc
  32.  
  33. while (amountLeftToPay > 0 && myBills[i] > 0)
  34. {
  35. amountLeftToPay -= billValues[i];
  36. myBills[i]--;
  37. paidBills[i]++;
  38. Total -= billValues[i];
  39. }
  40. }
  41.  
  42. int Change = amountLeftToPay *-1;
  43.  
  44. Console.WriteLine("1$: " + myBills[0]);
  45. Console.WriteLine("5$: " + myBills[1]);
  46. Console.WriteLine("10$: " + myBills[2]);
  47. Console.WriteLine("20$: " + myBills[3]);
  48. Console.WriteLine("50$: " + myBills[4]);
  49. Console.WriteLine("100$: " + myBills[5]);
  50. Console.WriteLine("Total$: " +Total);
  51. Console.WriteLine("Change: "+Change);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement