国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java編寫的24點紙牌游戲

2019-11-26 15:14:12
字體:
來源:轉載
供稿:網友

任意4個1-13數字,加減乘除計算24點。

實現原理:

1)排列組合4個數字
2)計算每次排列組合的可能性

Cal24.java

import java.util.HashSet;import java.util.Set;  public class Cal24 {   private static final double precision = 0.00001;  private static final int target = 24;      public String[] execute(String[] inputs) {    int[] digits = new int[4];    for (int i = 0; i < inputs.length; i++) {      digits[i] = Integer.valueOf(inputs[i]);    }    return new String[]{calc(digits)};  }         private String calc(final int data[]){    final Set<String> out = new HashSet<String>();    Combination digit = new Combination() {             @Override      protected void handle(int[] result) {        final int[] r = result;         Combination oper = new Combination(){          @Override          protected void handle(int[] c) {            double x = r[0];            for (int i = 0; i < r.length - 1; i++) {              x = doCalculate(x, r[i + 1], c[i]);            }            if(Math.abs(Math.abs(x) - target) < precision || Math.abs(Math.abs(1/x) - target) < precision){              StringBuilder sb = new StringBuilder();              for (int j = 0; j < r.length; j++) {                sb.append(r[j]);                if(j != r.length - 1){                  sb.append(getOperation(c[j]));                }              }              out.add(sb.toString());            }                       }        };        oper.combine(new int[]{0, 1, 2, 3}, data.length - 1, true);               }    };         digit.combine(data);         StringBuilder sb = new StringBuilder();    for (String string : out) {      sb.append(string);      sb.append("/n");    }    return sb.toString();  }              private double doCalculate(double x, double y, int operation){    switch (operation) {    case 0:      return x + y;    case 1:      return x - y;    case 2:      return x * y;    case 3:      return x / y;    default:      return 0;    }  }     private static String getOperation(int operation){    switch (operation) {    case 0:      return "+";    case 1:      return "-";    case 2:      return "*";    case 3:      return "/";    default:      return "";    }  }    public static void main(String[] args) {    System.out.println(new Cal24().calc(new int[]{1, 5, 5, 5}));  }}

Combination.java

public abstract class Combination {  private boolean repeat;  private int total = 0;     public void combine(int data[]){    combine(data, data.length, false);  }     public void combine(int data[], int count){    combine(data, count, false);  }     public void combine(int data[], int count, boolean repeat){    this.repeat = repeat;    int times = data.length;    int size = (int)Math.pow(times, count);    for (int i = 0; i < size; i++) {      int[] result = toArray(data, i, count);      if(result != null){        handle(result);        total ++;      }    }  }        private int[] toArray(int data[], int i, int count){    int [] indices = new int[count];    int times = data.length;    for (int j = 0; j < count; j++) {      int temp = 0;      if(i > 0){        temp = i%times;        i = (i - temp)/times;      }      indices[j] = temp;    }              if(!repeat){      //remove repetition      for (int x = 0; x < count; x++) {        for(int y = 0; y < count; y++){          if(x != y){            if(indices[x] == indices[y])              return null;          }        }      }    }         int [] result = new int[count];    for (int x = 0; x < count; x++) {      int selected = data[indices[x]];      result[x] = selected;    }         return result;  }     public int getTotal() {    return total;  }  protected abstract void handle(int[] result);}

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 元朗区| 元谋县| 阿荣旗| 砚山县| 涞源县| 四平市| 泽州县| 通江县| 濮阳县| 闸北区| 嘉义市| 安阳县| 广昌县| 虞城县| 新巴尔虎左旗| 汶川县| 渝北区| 塘沽区| 电白县| 松溪县| 香河县| 凤阳县| 峡江县| 鄄城县| 齐河县| 宝兴县| 荔浦县| 福泉市| 灵川县| 张家港市| 宁强县| 隆子县| 祁东县| 天台县| 四会市| 南川市| 内黄县| 凌源市| 阿鲁科尔沁旗| 遂平县| 上饶县|