2011年度最变态迷宫数学题-java

import java.util.ArrayList;
/**
 * 2011年度最变态迷宫数学题
 * @author Justplay(HBJ)
 *
 */
public class Arithmetic  {
	
	private int d;
	public ArrayList<String> arithmeticArray = new ArrayList<String>();
	private int count;
	
	public Arithmetic (int deep){
		this.d = deep;
	}
	
	public void add7(int value ,int deep,boolean left,String load){//  +7 
		if(deep>d){
			System.out.println("("+deep + ")("+count + ") N此路算法过长: "+load);
			return;
		}
		int tem = value+7;
		load += "+"+7;
		String _load = load;
		deep ++;
		if(left){
			this.ride3(tem, deep,true,_load);
			this.dim5(tem, deep,true,_load);
			if(tem%2==0){
				this.divide2(tem, deep,false,_load);
			}
		}else{
			if(tem%2==0){
				if(value==2012){
					count++;
					System.out.println("-----------------------Y此路算法通行: "+load);
					this.arithmeticArray.add(load);
					return;
				}
				this.divide2(tem, deep,true,_load);
			}
		}
	}
	
	public void divide2(int value ,int deep,boolean left,String load){//  /2
		if(deep>d){
			System.out.println("("+deep + ")("+count + ") N此路算法过长: "+load);
			return;
		} 
		int tem = value /2;
		load += "/"+2;
		String _load = load;
		deep++;
		if(left){
			this.ride3(tem, deep,true,_load);
			this.dim5(tem, deep,true,_load);
			this.add7(tem, deep, false,_load);
		}else{
			if(value==2012){
				count++;
				System.out.println("-----------------------Y此路算法通行: "+load);
				this.arithmeticArray.add(load);
				return;
			}
			this.add7(tem, deep, true,_load);
		}
	}
	
	public void ride3(int value ,int deep,boolean left,String load){//X3 
		if(deep>d){
			System.out.println("("+deep + ")("+count + ") N此路算法过长: "+load);
			return;
		}
		int tem = value * 3 ;
		load += "X"+3;
		String _load = load;
		deep++;
		if(left){
			if(tem==2012){
				count++;
				System.out.println("-----------------------Y此路算法通行: "+load);
				this.arithmeticArray.add(load+"  $(deep:"+deep+")");
				return;
			}
			this.dim5(tem, deep, false,_load);
		}else{
			this.add7(tem, deep, false,_load);
			this.dim5(tem, deep, true,_load);
			if(tem%2==0){
				this.divide2(tem, deep, false,_load);
			}
		}
	}
	
	public void dim5(int value ,int deep,boolean left,String load){ //-5
		if(deep>d){
			System.out.println("("+deep + ")("+count + ") N此路算法过长: "+load);
			return;
		}
		int tem = value - 5 ;
		load += "-"+5;
		String _load = load;
		deep++;
		if(left){
			if(tem==2012){
				count++;
				System.out.println("-----------------------Y此路算法通行: "+load);
				this.arithmeticArray.add(load+"  $(深度:"+deep+")");
				return;
			}
			this.ride3(tem, deep, false,_load);
		}else{
			this.add7(tem, deep, false,_load);
			this.ride3(tem, deep, true,_load);
			if(tem%2==0){
				this.divide2(tem, deep, false,_load);
			}
		}
	}
	
	public static void main(String args[]){
		Arithmetic  a = new Arithmetic (28);
		a.add7(2011, 0, true,"");
		System.out.println("结果集合");
		for(String s :a.arithmeticArray){
			System.out.println(s);
		}
//		+7/2+7/2+7-5X3/2+7/2+7X3-5/2+7/2+7-5X3-5X3/2+7-5X3/2+7-5
//		int i = (((((((((((((2011+7)/2+7)/2+7-5)*3)/2+7)/2+7)*3-5)/2+7)/2+7-5)*3-5)*3)/2+7-5)*3)/2+7-5;
//		System.out.println(i);
	}
}

转载请注明:http://justplayoop1.iteye.com/admin/blogs/1292567

你可能感兴趣的:(java)