にじゅうてんアルゴリズム



package com.onezero;
/**
 * <b>  24  </b>
 * <br/>
 *     1 13     ,  +、-、*、/、()              24;
 *    1、3、4、6,     6/(1-(3/4))
 * <br/>
 *         ,            。
 *           ,    <code>   </code> 。
 *     :          , 6 ,         , 5 ;
 *                   。     , 3 ;
 *    ,  5 ,      ,   ,  5 ;           24  。
 *   6*5*3*5*5=2250
 * <br/>
 * <b>            </b>
 * <br/>
 * <b>    :</b><code>com.onezero. 24.     (new int[]{1,3,4,6})</code>
 * @see com.onezero.   
 * @version 	1.0, 2009 1 18 
 * @author Anguo Wen
 *
 */
public final class  24 {
	private static int[]     = { 0, 1, 2, 3,  0, 2, 1, 3,  0, 3, 1, 2,  
		1, 2, 0, 3,  1, 3, 0, 2,  2, 3, 0, 1 };
	private static int[]     = { 0, 1, 2,  0, 2, 1,  1, 2, 0 };
	private static StringBuilder    ;
	
	/**
	 *      ,    ,       (       )
	 * 
	 * @param          1 13    
	 * @return       24
	 * @throws  ArithmeticException       <code>   </code>      .
	 */
	public static boolean      (final int[]    ) {
		if(   .length<4)throw new ArithmeticException("      ");
		   []    = new    [4];
		for(int h=0;h<4;h++)  [h] = new    (   [h]);
		boolean    = false;
		   []    = new    [4];
		String[]    = new String[4];
		      ;
		  : for (int i = 0; i <    .length; i += 4) {
			for (int t = 0; t < i; t += 4) {
				if (  (  [   [i]],   [   [i + 1]],   [   [t]],   [   [t + 1]]))
					continue   ;
			}
			for (int j = 0; j < 5; j++) {
				if (  (  [   [i]],   [   [i + 1]], j))
					continue;
				  [0] =   (  [   [i]],   [   [i + 1]], j,
						  [   [i]].toString(),   [   [i + 1]].toString());
				  [0] =    .toString();
				  [1] =   [   [i + 2]];
				  [1] =   [1].toString();
				  [2] =   [   [i + 3]];
				  [2] =   [2].toString();

				  : for (int k = 0; k <    .length; k += 3) {
					for (int s = 0; s < k; s += 3) {
						if (  (  [   [k]],   [   [k + 1]],   [   [s]],
								  [   [s + 1]]))
							continue   ;
					}

					if (k >= 6)
						for (int r = 0; r < i; r += 4) {
							if (  (  [   [k]],   [   [k + 1]],   [   [r]],
									  [   [r + 1]]))
								continue   ;
						}
					for (int l = 0; l < 5; l++) {
						if (  (  [   [k]],   [   [k + 1]], l))
							continue;
						  [3] =   (  [   [k]],   [   [k + 1]], l,   [   [k]],
								  [   [k + 1]]);
						  [3] =    .toString();
						for (int m = 0; m < 5; m++) {
							if (  (  [   [k + 2]],   [3], m))
								continue;
							   =   (  [   [k + 2]],   [3], m,   [   [k + 2]],
									  [3]);

							if (  .  (24)) {
								System.out.print(   .substring(1,
										   .length() - 1) + "\t");
								   = true;
							}
						}

					}
				}
			}
		}
		return   ;
	}

	private static boolean   (      ,       ,       ,       ) {
		return (  .equals(  ) &&   .equals(  ))
				|| (  .equals(  ) &&   .equals(  ));
	}

	private static boolean   (      ,       , int    ) {
		switch (   ) {
		case 1:
			return   .equals(  );
		case 2:
			return   .  (2) &&   .  (2);
		case 3:
			return   .  (1);
		case 4:
			return   .  (1) ||   .equals(  );
		default:
			return false;
		}
	}

	private static       (       ,        , int    , String   , String   ) {
		    = new StringBuilder("(");
		switch (   ) {
		case 0://    +   
			   .append(  ).append("+").append(  ).append(")");
			return    . (   );
		case 1:// |   -   |
			       =    . (   );
			if (  .   ()) {
				  . ();
				   .append(  ).append("-").append(  ).append(")");
			} else 
				   .append(  ).append("-").append(  ).append(")");
			return   ;
		case 2://    *   
			   .append(  ).append("*").append(  ).append(")");
			return    . (   );
		case 3://    /   
			   .append(  ).append("/").append(  ).append(")");
			return    . (   );
		default://    /   
			   .append(  ).append("/").append(  ).append(")");
			return    . (   );
		}
	}
	
	/**
	 *        
	 *                     
	 *     java -cp 24   .jar com.onezero. 24
	 * @param args
	 */
	public static void main(String[] args) {
		int    = 0;
		for (int i = 1; i < 14; i++)
			for (int j = i; j < 14; j++)
				for (int k = j; k < 14; k++)
					for (int l = k; l < 14; l++)
						if (     (new int[] { i, j, k, l })) {
							System.out.println();
							  ++;
						}
		System.out.println(  );
	}
}

class     {
	private int   ;
	private int    = 1;

	   (int   ) {
		this.   =   ;
	}

	   (int   , int   ) {
		if (   <= 0)
			throw new ArithmeticException("         !");
		int     =      (   < 0 ? -   :   ,   );
		this.   =    /    ;
		this.   =    /    ;
	}

	public boolean   (int   ) {
		return    == this.   && 1 == this.  ;
	}

	private int      (int   , int   ) {
		if (   ==   ) {
			if (   == 0)
				throw new ArithmeticException("            !");
			return   ;
		}
		if (   == 0)
			return   ;
		if (   == 0)
			return   ;
		else if (   >   )
			return      (  ,    %   );
		else
			return      (  ,    %   );
	}

	public      (       ) {
		return new    (this.   *    .   + this.   *    .  , this.   *    .  );
	}

	public      (       ) {
		return new    (this.   *    .   - this.   *    .  , this.   *    .  );
	}

	public      (       ) {
		return new    (this.   *    .  , this.   *    .  );
	}

	public      (       ) {
		if (   .   == 0)
			throw new ArithmeticException("    !");
		return new    (this.   *    .  , this.   *    .  );
	}

	public boolean    () {
		return this.   < 0;
	}

	public void  () {
		this.   = -this.  ;
	}

	public String toString() {
		if (this.   == 1)
			return Integer.toString(this.  );
		return this.   + "/" + this.  ;
	}

	public boolean equals(Object  ) {
		if (!(  instanceof    ))
			return false;
		       = (   )  ;
		return this.   ==   .   && this.   ==   .  ;
	}
}