View Code of Problem 29

import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int[] stuff = new int[9];
		for(int i =0;i<9;i++) {
			stuff[i]=in.nextInt();
		}
		int[] choise = mokechoise(stuff);
		for(int i =0;i<choise.length;i++) {
			if(choise[i]!=0) {
				for(int j =0;j<choise[i];j++) {
					System.out.print((i+1)+" ");
				}
			}
		}
	}
	public static int[] buy(int[] have,int mode,int number) {
		for(int i =0;i<number;i++) {
			if(mode == 1) {
				have[0]++;
				have[1]++;
				have[3]++;
				have[4]++;
			}else if(mode == 2){
				have[0]++;
				have[1]++;
				have[2]++;
			}else if(mode == 3){
				have[1]++;
				have[2]++;
				have[4]++;
				have[5]++;
			}else if(mode == 4){
				have[0]++;
				have[3]++;
				have[6]++;
			}else if(mode == 5){
				have[1]++;
				have[3]++;
				have[4]++;
				have[5]++;
				have[7]++;
			}else if(mode == 6){
				have[2]++;
				have[5]++;
				have[8]++;
			}else if(mode == 7){
				have[3]++;
				have[4]++;
				have[6]++;
				have[7]++;
			}else if(mode == 8){
				have[6]++;
				have[7]++;
				have[8]++;
			}else if(mode == 9){
				have[4]++;
				have[5]++;
				have[7]++;
				have[8]++;
			}
		}
		return have;
	}
	
	public static int[] mokechoise(int[] have) {
		int[] copy = have.clone();
		int number[] = new int[9];
		int max =2;
		while(true) {
			for(int n1 = 0;n1<max;n1++) {
				for(int n2 = 0;n2<max;n2++) {
					for(int n3 = 0;n3<max;n3++) {
						for(int n4 = 0;n4<max;n4++) {
							for(int n5 = 0;n5<max;n5++) {
								for(int n6 = 0;n6<max;n6++) {
									for(int n7 = 0;n7<max;n7++) {
										for(int n8 = 0;n8<max;n8++) {
											for(int n9 = 0;n9<max;n9++) {
												buy(copy,1,n1);
												buy(copy,2,n2);
												buy(copy,3,n3);
												buy(copy,4,n4);
												buy(copy,5,n5);
												buy(copy,6,n6);
												buy(copy,7,n7);
												buy(copy,8,n8);
												buy(copy,9,n9);
												boolean bl = true; 
												for(int i :copy) {
													if(i%4!=0) {
														bl=false;
														break;
													}
												}
												if(bl) {
													number[0]=n1;
													number[1]=n2;
													number[2]=n3;
													number[3]=n4;
													number[4]=n5;
													number[5]=n6;
													number[6]=n7;
													number[7]=n8;
													number[8]=n9;
													return number;
												}else {
													copy=have.clone();
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			max++;
		}
	}
}

Double click to view unformatted code.


Back to problem 29