Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Präsenzübung2;
- import java.util.Scanner;
- class Coin{
- private int Wert;
- private boolean give;
- Coin(int W){
- Wert=W;
- give=false;
- }
- int getWert() {
- return Wert;
- }
- void setgiven(boolean b) {
- give=b;
- }
- boolean git() {
- return give;
- }
- }
- public class Erbe {
- 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)};
- private static int Wert=Vermögen(Schatz)/2;
- public static int Vermögen(Coin[] Schatz) {
- int c=0;
- int Sum=0;
- while(c<Schatz.length) {
- Sum+=Schatz[c].getWert();
- c++;
- }
- return Sum;
- }
- public static boolean Vert(int Sum, int Stelle) {
- if ((Sum+Schatz[Stelle].getWert())>Wert) return false;
- if ((Sum+Schatz[Stelle].getWert())==Wert) {
- Schatz[Stelle].setgiven(true);
- return true;
- }
- if (Stelle==Schatz.length-1) return false;
- if (Vert(Sum,Stelle+1)) {
- Schatz[Stelle].setgiven(false);
- return true;
- }
- if (Vert(Sum+Schatz[Stelle].getWert(),Stelle+1)) {
- Schatz[Stelle].setgiven(true);
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- System.out.println(Vermögen(Schatz));
- if (Vermögen(Schatz)%2!=0) System.out.println(false);
- else{
- if (Vert(0,0)) {
- Verteilung();
- }
- else System.out.println("Pech gehabt");
- }
- }
- public static void Verteilung() {
- int c=0;
- boolean b=false;
- System.out.println("Erbe 1:");
- while(c<Schatz.length) {
- b=Schatz[c].git();
- if(b) {
- System.out.println("+"+Schatz[c].getWert()+" ["+(++c)+"]");
- }
- else c++;
- }
- System.out.println("Erbe 2:");
- c=0;
- while(c<Schatz.length) {
- b=Schatz[c].git();
- if(!b) {
- System.out.println("+"+Schatz[c].getWert()+" ["+(++c)+"]");
- }
- else c++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement