ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
1247
1247
EMPFEHLEN
-
05.04.10 21:24 #1
- Registriert seit
- Aug 2001
- Ort
- Österreich, Stmk, Graz
- Beiträge
- 2.783
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)); } } } }
With the first link the chain is forged. The first speech censored, the first thought forbidden, the first freedom denied, chains us all irrevocably.
Aaron Satie
Legends... are the spice of the universe, Mr. Data, because they have a way of sometimes coming true.
Captain Jean-Luc Picard, Stardate ~41294.5
Tutorials.de chattet. Hier gibts auch .net Support ^^
Klickt auf chattet und nutzt den Webchat, oder verbindet euch zu irc.tutorials.de - Channel #Tutorials.de
(moo)blog furred.net // SiteInfo für WP7 // Pastebin für WP7 // BlogEngine.net Extensions
Ähnliche Themen
-
[Quiz#14] Alexander Schuc (C#)
Von Alexander Schuc im Forum ArchivAntworten: 1Letzter Beitrag: 28.03.10, 14:22 -
[Quiz#13] Alexander Schuc (C#)
Von Alexander Schuc im Forum ArchivAntworten: 0Letzter Beitrag: 17.01.10, 18:38 -
[Quiz#11] Alexander Schuc (C#)
Von Alexander Schuc im Forum ArchivAntworten: 2Letzter Beitrag: 24.10.09, 23:24 -
[QUIZ#02] Alexander Schuc (C#)
Von Alexander Schuc im Forum ArchivAntworten: 0Letzter Beitrag: 28.09.08, 14:59 -
[QUIZ#1] Alexander Schuc (C#)
Von Alexander Schuc im Forum ArchivAntworten: 0Letzter Beitrag: 18.09.08, 23:49






Login





