Advertisement
Randomsurpriseguy

Erben

Feb 8th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package Präsenzübung2;
  2. import java.util.Scanner;
  3.  
  4.  
  5. class Coin{
  6.     private int Wert;
  7.     private boolean give;
  8.    
  9.    
  10.    
  11.     Coin(int W){
  12.         Wert=W;
  13.         give=false;
  14.     }
  15.    
  16.     int getWert() {
  17.         return Wert;
  18.     }
  19.    
  20.     void setgiven(boolean b) {
  21.         give=b;
  22.     }
  23.    
  24.     boolean git() {
  25.         return give;
  26.     }
  27. }
  28.  
  29.  
  30. public class Erbe {
  31.    
  32.     private static Coin[] Schatz=new Coin[] {new Coin(2),new Coin(3),new Coin(1),new Coin(4),new Coin(3),new Coin(4),new Coin(6),new Coin(10),new Coin(3),new Coin(1),new Coin(2),new Coin(2),new Coin(2),new Coin(2),new Coin(5)};//{new Coin(2),new Coin(3),new Coin(5),new Coin(5),new Coin(8),new Coin(1)};
  33.    
  34.     private static int Wert=Vermögen(Schatz)/2;
  35.  
  36.    
  37.     public static int Vermögen(Coin[] Schatz) {
  38.         int c=0;
  39.         int Sum=0;
  40.         while(c<Schatz.length) {
  41.             Sum+=Schatz[c].getWert();
  42.             c++;
  43.         }
  44.         return Sum;
  45.     }
  46.    
  47.     public static boolean Vert(int Sum, int Stelle) {
  48.         if ((Sum+Schatz[Stelle].getWert())>Wert) return false;
  49.         if ((Sum+Schatz[Stelle].getWert())==Wert) {
  50.             Schatz[Stelle].setgiven(true);
  51.             return true;
  52.         }
  53.         if (Stelle==Schatz.length-1) return false;
  54.         if (Vert(Sum,Stelle+1)) {
  55.             Schatz[Stelle].setgiven(false);
  56.             return true;
  57.         }
  58.         if (Vert(Sum+Schatz[Stelle].getWert(),Stelle+1)) {
  59.             Schatz[Stelle].setgiven(true);
  60.             return true;
  61.         }
  62.         return false;
  63.     }
  64.    
  65.     public static void main(String[] args) {
  66.         System.out.println(Vermögen(Schatz));
  67.         if (Vermögen(Schatz)%2!=0) System.out.println(false);
  68.         else{
  69.             if (Vert(0,0)) {
  70.                 Verteilung();
  71.             }
  72.             else System.out.println("Pech gehabt");
  73.         }
  74.        
  75.     }
  76.    
  77.     public static void Verteilung() {
  78.         int c=0;
  79.         boolean b=false;
  80.         System.out.println("Erbe 1:");
  81.         while(c<Schatz.length) {
  82.             b=Schatz[c].git();
  83.             if(b) {
  84.                 System.out.println("+"+Schatz[c].getWert()+" ["+(++c)+"]");
  85.             }
  86.             else c++;
  87.         }
  88.         System.out.println("Erbe 2:");
  89.         c=0;
  90.         while(c<Schatz.length) {
  91.             b=Schatz[c].git();
  92.             if(!b) {
  93.                 System.out.println("+"+Schatz[c].getWert()+" ["+(++c)+"]");
  94.             }
  95.             else c++;
  96.            
  97.            
  98.         }
  99.     }
  100.    
  101.    
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement