Created
July 21, 2020 22:40
-
-
Save nothke/19d972b11f9aa91d8307af1a8f03c73f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| class Program | |
| { | |
| static List<int> pare; | |
| static void Main(string[] args) | |
| { | |
| pare = new List<int>(); | |
| Console.WriteLine("Unesi pare koje imas, ako je to-to, pritisni enter na prazno:"); | |
| while (true) | |
| { | |
| string str = Console.ReadLine(); | |
| if (string.IsNullOrEmpty(str)) | |
| break; | |
| else if (int.TryParse(str, out int inPare)) | |
| pare.Add(inPare); | |
| else | |
| Console.WriteLine("Nisu pare to lele. Probaj opet.."); | |
| } | |
| // Sort from largest to smallest | |
| pare.Sort(); | |
| pare.Reverse(); | |
| Console.WriteLine("Ok, imas: "); | |
| for (int i = 0; i < pare.Count; i++) | |
| { | |
| Console.Write(pare[i]); | |
| if (i != pare.Count - 1) | |
| Console.Write(", "); | |
| } | |
| Console.WriteLine("\n\nUnesi sumu koju oces da platis:"); | |
| int target; | |
| while (true) | |
| { | |
| string sumaStr = Console.ReadLine(); | |
| if (int.TryParse(sumaStr, out target)) | |
| break; | |
| else | |
| Console.WriteLine("Nije broj, probaj opet"); | |
| } | |
| Console.WriteLine(""); | |
| int total = 0; | |
| foreach (var para in pare) | |
| total += para; | |
| // Early out if not enough | |
| if (total < target) | |
| { | |
| Console.WriteLine("Eeee nemas dovoljno!"); | |
| Console.ReadKey(); | |
| return; | |
| } | |
| List<int> result = new List<int>(); | |
| int remaining = target; | |
| int lastOverlapRemaining = target; | |
| int currentIndex = 0; | |
| int lastOverlappingIndex = 0; | |
| // go down the stack and add everything that fits | |
| do | |
| { | |
| int next = pare[currentIndex]; | |
| if (next <= remaining) | |
| { | |
| result.Add(next); | |
| remaining -= next; | |
| } | |
| else | |
| { | |
| // remember the index of last that that didn't fit in case of kusur | |
| lastOverlappingIndex = currentIndex; | |
| lastOverlapRemaining = remaining; | |
| } | |
| currentIndex++; | |
| } | |
| while (currentIndex < pare.Count && remaining != 0); | |
| // KUSUR: | |
| // if we still have the remainder, we can pay, but kusur | |
| if (remaining > 0) | |
| { | |
| // trace back through the stack and pop those that are less than remaining | |
| for (int i = result.Count - 1; i >= 0; i--) | |
| { | |
| if (result[i] < lastOverlapRemaining) | |
| result.RemoveAt(result.Count - 1); | |
| else | |
| break; | |
| } | |
| result.Add(pare[lastOverlappingIndex]); | |
| int kusur = pare[lastOverlappingIndex] - lastOverlapRemaining; | |
| // Output | |
| Console.WriteLine("Jbg, ne fituje, ali mozes da platis sa:"); | |
| for (int i = 0; i < result.Count; i++) | |
| { | |
| Console.Write(result[i]); | |
| if (i != result.Count - 1) | |
| Console.Write(", "); | |
| } | |
| Console.WriteLine("\nI dobijes kusur: " + kusur); | |
| Console.ReadKey(); | |
| return; | |
| } | |
| Console.WriteLine("Mozes da platis sa:"); | |
| for (int i = 0; i < result.Count; i++) | |
| { | |
| Console.Write(result[i]); | |
| if (i != result.Count - 1) | |
| Console.Write(", "); | |
| } | |
| Console.WriteLine(); | |
| Console.ReadKey(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment