Benutzer-Werkzeuge

Webseiten-Werkzeuge


bwinf:bwinf40a5:start

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Nächste Überarbeitung
Vorhergehende Überarbeitung
bwinf:bwinf40a5:start [2023/06/30 07:23] – angelegt Martin Pabstbwinf:bwinf40a5:start [2023/07/03 10:27] (aktuell) Martin Pabst
Zeile 8: Zeile 8:
  
 <script type="text/plain" title="Main Program.java"> <script type="text/plain" title="Main Program.java">
 +new Hauptprogramm("gewichtsstücke0.txt").start();
  
 +
 +
 +
 +class Hauptprogramm {
 +
 +   Data data;
 +
 +   boolean[] istMessbar;   // istMessbar[i] == true <=> i * 10g messbar sind
 +
 +
 +   Hauptprogramm(String filename) {
 +      data = new Data(filename);
 +
 +      istMessbar = new boolean[1001];  // anfangs mit [false, false, false, ... ] belegt.
 +
 +   }
 +
 +
 +   void start() {
 +      legeHinzu(0, 0);
 +      ausgabe(); 
 +   }
 +
 +
 +
 +   void legeHinzu(int indexNächstesGewicht, int bisherigesGesamtgewicht) {
 +
 +      int anzahlAktuellesGewicht = data.anzahlListe[indexNächstesGewicht];
 +
 +      for (int i = -anzahlAktuellesGewicht; i <= anzahlAktuellesGewicht; i++) {
 +
 +         int neuesGesamtgewicht = bisherigesGesamtgewicht + i * data.gewichte[indexNächstesGewicht];
 +
 +         if(neuesGesamtgewicht > 0 && neuesGesamtgewicht <= 10000 && neuesGesamtgewicht % 10 == 0) {
 +            istMessbar[neuesGesamtgewicht / 10] = true;
 +         }
 +
 +         // Abbruchbedingung für die Rekursion
 +         if(indexNächstesGewicht < data.anzahl - 1) {
 +            legeHinzu(indexNächstesGewicht + 1, neuesGesamtgewicht); 
 +         }
 +
 +      }
 +
 +   }
 +
 +   void ausgabe() {
 +
 +      int count = 0;
 +
 +      for (int i = 0; i <= 1000; i++) {
 +         
 +         if(istMessbar[i]) {
 +            println(i * 10 + " g");
 +            count++;
 +         }
 +         
 +      }
 +
 +      println(count + " Gewichte im Bereich 10 g bis 10000 g sind mit dem vorgegebenen Gewichtssatz erreichbar.");
 +   }
 +
 +
 +}
 </script> </script>
  
 <script type="text/plain" title="Data.java"> <script type="text/plain" title="Data.java">
 +class Data {
 +   
 +   int anzahl;
 +   int[] gewichte;
 +   int[] anzahlListe;
 +
 +   Data(String filename) {
 +      String[] lines = Files.read(filename).split("\n");
 +      anzahl = Integer.parseInt(lines[0]);
 +      gewichte = new int[anzahl];
 +      anzahlListe = new int[anzahl];
 +
 +      for (int i = 0; i < anzahl; i++) {
 +         String line = lines[i + 1];
 +         String[] zahlen = line.split(" ");
 +         gewichte[i] = Integer.parseInt(zahlen[0]);
 +         anzahlListe[i] = Integer.parseInt(zahlen[1]);
 +      } 
 +
 +   }
 +}
 </script> </script>
  
bwinf/bwinf40a5/start.1688109817.txt.gz · Zuletzt geändert: 2023/06/30 07:23 von Martin Pabst

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki