Java实现24点纸牌游戏
2016-04-14 21:49
871 查看
任意4个1-13数字,使用运算符号使其计算结果为24点。
实现过程:
1)排列组合4个数字
2)计算每次排列组合的可能性
实现过程:
1)排列组合4个数字
2)计算每次排列组合的可能性
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})); } }
另外一个类
<pre name="code" class="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); }
相关文章推荐
- java中文乱码解决之道(八)-----解决URL中文乱码问题
- java中文乱码解决之道(七)-----JSP页面编码过程
- java中文乱码解决之道(六)-----javaWeb中的编码解码
- java中文乱码解决之道(五)-----java是如何编码解码的
- [SpringBoot实践]spring-data-mongo自定义Repository接口及其实现
- java中文乱码解决之道(四)-----java编码转换过程
- java中文乱码解决之道(三)-----编码详情:伟大的创想---Unicode编码
- java中文乱码解决之道(二)-----字符编码详解:基础知识 + ASCII + GB**
- java输入学生成绩
- java怎么连接mysql数据库
- java中文乱码解决之道(一)-----认识字符集
- (2)java自带软件包javax.crypto的使用方法,保存generator中生成的key
- java基础; 面向对象编程; 对象的实例化
- BZOJ 3198 SDOI2013 spring
- java中的内省(Introspector)
- Java状态机
- JAVA——匿名对象
- 20145305 《Java程序设计》第7周学习总结
- Java中的5种同步辅助类
- java 背后的小动作