Advertisement
Danielos168

Plecak(Zachłanne)

Nov 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zad4
  8. {
  9.     class Program
  10.     {
  11.         static List<int> plecak(List<int> przedmioty, int pojemnosc)
  12.         {
  13.             List<int> ile = new List<int>();
  14.             int bufor = 0;
  15.             while (pojemnosc>=przedmioty.Max())
  16.             {
  17.  
  18.                 ile.Add(przedmioty.Max());
  19.                 bufor = przedmioty.Max();
  20.                 pojemnosc -= bufor;
  21.                 przedmioty.Remove(przedmioty.Max());
  22.                
  23.             }
  24.  
  25.             return ile;
  26.         }
  27.         static void Main(string[] args)
  28.         {
  29.             Random r = new Random();
  30.             List<int> przedmioty = new List<int>(25);
  31.             for (int i = 0; i < 25; i++)
  32.             {
  33.                 przedmioty.Add(r.Next(0,100));
  34.             }
  35.  
  36.             foreach (var i in przedmioty)
  37.             {
  38.                 Console.Write(i + ",");
  39.             }
  40.             Console.WriteLine();
  41.             int pojemnosc = 1000;
  42.             List<int> wynik = plecak(przedmioty, pojemnosc);
  43.             int ilosc = 0;
  44.             foreach (var i in wynik)
  45.             {
  46.                 Console.Write(i + ",");
  47.                 ilosc += i;
  48.             }
  49.  
  50.             Console.WriteLine("Łączna waga wynosi: " + ilosc);
  51.  
  52.             Console.ReadKey();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement