zoukankan      html  css  js  c++  java
  • 24点游戏

    遍历法:

     1 public class Calculate {
     2 
     3     final double Threshold = 1E-6;
     4     final int CardsNumber = 4;
     5     final int ResultValue = 24;
     6     double number[] = new double[CardsNumber];
     7     String result[]=new String[CardsNumber];
     8     
     9     public boolean PointsGame(int n){
    10         if(n == 1){
    11             if(Math.abs(number[0]-ResultValue)<Threshold){
    12                  System.out.println(result[0]);
    13                  return true;
    14             }else{
    15                 return false;
    16             }
    17         }
    18         for(int i=0; i<n ; i++){
    19             for(int j=i+1; j<n; j++){
    20                 double a,b;
    21                 String expa, expb;
    22                 a = number[i];
    23                 b = number[j];
    24                 number[j] = number[n - 1];
    25                 
    26                 expa = result[i];
    27                 expb = result[j];
    28                 result[j] = result[n - 1];
    29                 
    30                 result[i] = "(" + expa + "+" + expb + ")";
    31                 number[i] = a + b;
    32                 if(PointsGame(n - 1))
    33                     return true;
    34                 
    35                 result[i] = "(" + expa + "-" + expb + ")";
    36                 number[i] = a - b;
    37                 if(PointsGame(n - 1))
    38                     return true;
    39                 
    40                 result[i] = "(" + expb + "-" + expa + ")";
    41                 number[i] = b - a;
    42                 if(PointsGame(n - 1))
    43                     return true;
    44                 
    45                 result[i] = "(" + expa + "*" + expb + ")";
    46                 number[i] = a * b;
    47                 if(PointsGame(n - 1))
    48                     return true;
    49                 
    50                 if(b!=0){
    51                     result[i] = "(" + expa + "/" + expb + ")";
    52                     number[i] = a / b;
    53                     if(PointsGame(n - 1))
    54                         return true;
    55                 }
    56                 
    57                 if(a!=0){
    58                 result[i] = "(" + expb + "/" + expa + ")";
    59                 number[i] = b - a;
    60                 if(PointsGame(n - 1))
    61                     return true;
    62                 }
    63                 
    64                 number[i] = a;
    65                 number[j] = b;
    66                 result[i] = expa;
    67                 result[j] = expb;
    68             }
    69         }
    70         return false;
    71     }
    72     /**
    73      * @param args
    74      */
    75     public static void main(String[] args) {
    76 
    77         Calculate ca = new Calculate();
    78         ca.number[0] = 11;
    79         ca.number[1] = 8;
    80         ca.number[2] = 3;
    81         ca.number[3] = 5;
    82         ca.result[0] = "11";
    83         ca.result[1] = "8";
    84         ca.result[2] = "3";
    85         ca.result[3] = "5";
    86         ca.PointsGame(4);
    87     }
    88 
    89 }
  • 相关阅读:
    作业
    awk 简单用法小结
    R/SAS/Matab数据相互转换
    Xargs 批量处理命令
    线性表
    C plus 笔记(一 )
    C plus 内存管理
    Skyeye学习(一)—— 安装与使用测试例子
    mongoDB基本操作
    《Algorithms Unlocked》读书笔记3——计数排序
  • 原文地址:https://www.cnblogs.com/chlde/p/2739043.html
Copyright © 2011-2022 走看看