Hoi,..

wie gesagt, die Lösung ist recht flott entstanden, und wie schon im Diskussiontthread besprochen ist dies keine genaue Lösung.

Generator für Süßigkeitenlisten ist auch dabei, dazu einfach dem Programm einen Parameter der die zu generierende Menge angibt übergeben, und die Ausgabe in eine Datei umleiten.

lg,..

Code csharp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CodingQuiz15
{
    public class Sweet
    {
        public string Name { get; set; }
        public int Weight { get; set; }
        public int Value { get; set; }
 
        private double? ratio;
        public double Ratio
        {
            get
            {
                if (ratio == null)
                    ratio = Value / (double)Weight;
 
                return ratio.Value;
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                generateList(int.Parse(args[0]));
                return;
            }
 
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
 
            List<Sweet> sweets = new List<Sweet>();
 
            int maxWeight = int.Parse(Console.ReadLine());
            string name;
            string[] data;
            while ((name = Console.ReadLine()) != null)
            {
                data = Console.ReadLine().Split(' ');
 
                sweets.Add(new Sweet
                {
                    Name=name,
                    Weight=int.Parse(data[0]),
                    Value=int.Parse(data[1])
                });
            }
 
 
            string auswahl = "";
            int currentWeight = 0;
            int value = 0;
            List<Sweet> nest = new List<Sweet>();
 
            System.Diagnostics.Stopwatch swatch = new System.Diagnostics.Stopwatch();
            swatch.Start();
 
            foreach (var s in sweets.OrderByDescending(i => i.Ratio))
            {
                if (currentWeight + s.Weight > maxWeight)
                    continue;
 
                currentWeight += s.Weight;
                auswahl += s.Name + ", ";
                value += s.Value;
            }
            
            swatch.Stop();
 
            auswahl = auswahl.TrimEnd(',', ' ');
 
            Console.WriteLine("(Nicht-)Optimale Auswahl: {0}\r\nMasse: {1} g\r\nNährwert: {2} kcal",
                auswahl, currentWeight, value);
 
            watch.Stop();
 
            Console.WriteLine("Took {0}ms // Selection in {1}ms", watch.ElapsedMilliseconds, swatch.ElapsedMilliseconds);
 
            Console.ReadLine();
        }
 
        private static void generateList(int count)
        {
            Random rnd = new Random();
 
            Console.WriteLine("500");
 
            for (int i = 0; i < count; i++)
            {
                Console.WriteLine("Eiei-{0}", i);
                Console.WriteLine("{0} {1}", rnd.Next(50, 100), rnd.Next(200, 1000));
            }
        }
    }
}