1到9加減乘除得到結果等於指定值


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 對於給出的一個數字N。
 * 找出從1到9這9個數字順序加減乘除組成的算式,
 * 這些算式的計算結果須恰好為N!
 * @author tiger
 * @date 2010年5月19日於公司
 * 信手而寫,極不規范。異日有暇,再行修繕。
 */
public class cumt {

 private Stack stack = new Stack();
 private Stack stack1 = new Stack();
 
 /**
  * 求算式str的結果
  * str必須是正確的算式!
  * str中不能有空格!
  *
  * 用堆棧來執行計算。
  * 第一次,遍歷字符串,執行乘除運算,把算式順序壓入堆棧中。
  * 倒置堆棧(借用了第二個堆棧來處理)
  * 第二次,遍歷堆棧,執行加減運算。最后得到結果。
  * @return
  */
 private int action(String str)
 {
  int count = 0;
  int type = 0; // 0: 加法,1:減法
  for (int i = 0; i < str.length(); i++) {
   String c = str.substring(i , i+1);
   if(!c.equals("*") && !c.equals("/"))
   {
    stack.push(c);
   }else{
    String c1 = (String) stack.pop();
    int k = Integer.parseInt(str.substring(i + 1 , i+2)) ;
    if(c.equals("*"))
    {
     k = k * Integer.parseInt(c1);
    }else if(c.equals("/"))
    {
     k = Integer.parseInt(c1) / k;
    }
    stack.push(k + "");
    i++;
   }
  }
  
  while(!stack.isEmpty())
  {
   stack1.push(stack.pop());
  }
  
  count = Integer.parseInt((String)stack1.pop());
  while(!stack1.isEmpty()){
   String c = (String)stack1.pop();
   if(c.equals("+") || c.equals("-"))
   {
    type = c.equals("+")? 0: 1;
   }else{
    if(type == 0)
    {
     count += Integer.parseInt(c);
    }else if(type == 1)
    {
     count -= Integer.parseInt(c);
    }
   }
  }
  
  return count;
 }
 
 /**
   * 得到正整數a的4進制表示
   */
  public String getFourString(int a){
   String str1 = "0123";
   String str = "";
   while(a>0){
    int k = a & 0x3;  // 相當於 int k = a % 4;
    str = str1.charAt(k)+str;
    a = a >> 2;       // 相當於 a = a / 4;
   }
   return str;
  }
 
 
  /**
   * 將字符串補足到8位
   * 不足位補0,0為加法運算。
   * 因為很多的數的四進制字符串不足8位。
   */
 private String buzu(String str)
 {
  if(str.length() >= 8)return str;
  String str1 = str;
  while(str1.length() < 8)
  {
   str1 = "0" + str1;
  }
  return str1;
 }
 
 /**
   * 主要邏輯(思路如下:)
   * 通過四進制字符串來得到加減乘除四種運算的全排列字符串
   * 將該字符串與1-9這9個數字合並成算式。共有4的8次方個算式
   * 計算這些算式的結果,如果與目標值相等,打印之!
   */
 private void logic(int abc)
 {
  for (int i = 0; i < Math.pow(4, 8); i++) {
   String str = this.getFourString(i);
   str = this.buzu(str);
   
   String str1 = "1";
   for (int j = 0; j < 8; j++){
    String s = str.charAt(j)+"";
    if(s.equals("0"))
    {
     str1 += "+"+(j+2);
    }else if(s.equals("1"))
    {
     str1 += "-"+(j+2);
    }else if(s.equals("2"))
    {
     str1 += "*"+(j+2);
    }else if(s.equals("3"))
    {
     str1 += "/"+(j+2);
    }
   }
   if(this.action(str1) == abc)
   {
    System.out.println(str1 + "=" + abc);
   }
    
  }
 }
 
 /**
  * 程序入口
  */
 public static void main(String[] args) {
  //test : 求算式
//  String str = "1+2+3+4/5-6-7-8/9";
//  int a = new cumt().action(str);
//  System.out.println(str + "=" + a);
  
  //test : 求4進制
//  String a = new cumt().getFourString(100);
//  System.out.println(a);
  
  new cumt().logic(16);
 }
 
 
}

/*打印結果如下:
 1+2+3+4+5+6+7+8*9=100
 1+2+3-4*5+6*7+8*9=100
 1+2+3*4*5+6*7*8/9=100
 1+2-3*4+5*6+7+8*9=100
 1+2-3*4-5+6*7+8*9=100
 1+2*3+4*5-6+7+8*9=100
 1+2*3+4*5/6*7+8*9=100
 1+2*3*4*5/6+7+8*9=100
 1+2/3+4+5*6+7*8+9=100
 1+2/3+4+5*6-7+8*9=100
 1-2+3*4*5+6*7+8-9=100
 1-2+3*4*5-6+7*8-9=100
 1-2+3*4/5*6*7+8+9=100
 1-2*3+4*5+6+7+8*9=100
 1-2*3-4+5*6+7+8*9=100
 1-2*3-4-5+6*7+8*9=100
 1-2/3+4+5*6+7*8+9=100
 1-2/3+4+5*6-7+8*9=100
 1*2*3+4+5+6+7+8*9=100
 1*2*3-4*5+6*7+8*9=100
 1*2*3*4+5+6+7*8+9=100
 1*2*3*4+5+6-7+8*9=100
 1*2*3*4+5*6/7+8*9=100
*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM