编程之美1.1624点游戏解法一java版
2016-06-01 13:43
211 查看
书中给出的第一种解法的java版
根据给的思路可以得出是否能算出24的结论,但是没办法输出表达式
值得注意的地方:1.将两个取出来的数从原数组中去掉,代码中采用的方法很巧妙,用数组后面的值和新计算出的值来代替,这样做的好处是还原数组也很简单
2.输出表达式的地方还没想出怎么解决
扩展问题一:3,3,8,8程序给出的结论是false
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test;
import java.math.BigDecimal;
import static java.math.BigDecimal.ROUND_HALF_DOWN;
import java.util.*;
/**
*
* @author Administrator
*/
public class PointsGame {
static double[] number = {9, 9, 6, 2};
static String[] result =new String[4];
public static void main(String[] args) {
for(int i =0;i<4;i++){
result[i] ="";
}
boolean flag = pointsGame(4);
System.out.println("flag="+flag);
for(int i =0;i<4;i++){
System.out.println(result[i]);
}
}
private static boolean pointsGame(int n) {
if (n < 2) {
if (number[0] == 24) {
return true;
} else {
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
String expa,expb;
a = number[i];
b = number[j];
number[j]=number[n-1];
expa=result[i];
expb=result[j];
result[j]=result[n-1];
result[i]="("+expa+"+"+expb+")";
number[i] = a+b;
if(pointsGame(n-1))
return true;
result[i]="("+expa+"-"+expb+")";
number[i]=a-b;
if(pointsGame(n-1))
return true;
result[i]="("+expb+"-"+expa+")";
number[i]=b-a;
if(pointsGame(n-1))
return true;
result[i]="("+expa+"*"+expb+")";
number[i]=a*b;
if(pointsGame(n-1))
return true;
if(b!=0){
result[i]="("+expa+"/"+expb+")";
number[i]=a/b;
if(pointsGame(n-1))
return true;
}
if(a!=0){
result[i]="("+expb+"/"+expa+")";
number[i]=b/a;
if(pointsGame(n-1))
return true;
}
number[i]=a;
number[j]=b;
result[i]=expa;
result[j]=expb;
}
}
return false;
}
}
运行结果:true (((+)-)*)
根据给的思路可以得出是否能算出24的结论,但是没办法输出表达式
值得注意的地方:1.将两个取出来的数从原数组中去掉,代码中采用的方法很巧妙,用数组后面的值和新计算出的值来代替,这样做的好处是还原数组也很简单
2.输出表达式的地方还没想出怎么解决
扩展问题一:3,3,8,8程序给出的结论是false
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Test;
import java.math.BigDecimal;
import static java.math.BigDecimal.ROUND_HALF_DOWN;
import java.util.*;
/**
*
* @author Administrator
*/
public class PointsGame {
static double[] number = {9, 9, 6, 2};
static String[] result =new String[4];
public static void main(String[] args) {
for(int i =0;i<4;i++){
result[i] ="";
}
boolean flag = pointsGame(4);
System.out.println("flag="+flag);
for(int i =0;i<4;i++){
System.out.println(result[i]);
}
}
private static boolean pointsGame(int n) {
if (n < 2) {
if (number[0] == 24) {
return true;
} else {
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
String expa,expb;
a = number[i];
b = number[j];
number[j]=number[n-1];
expa=result[i];
expb=result[j];
result[j]=result[n-1];
result[i]="("+expa+"+"+expb+")";
number[i] = a+b;
if(pointsGame(n-1))
return true;
result[i]="("+expa+"-"+expb+")";
number[i]=a-b;
if(pointsGame(n-1))
return true;
result[i]="("+expb+"-"+expa+")";
number[i]=b-a;
if(pointsGame(n-1))
return true;
result[i]="("+expa+"*"+expb+")";
number[i]=a*b;
if(pointsGame(n-1))
return true;
if(b!=0){
result[i]="("+expa+"/"+expb+")";
number[i]=a/b;
if(pointsGame(n-1))
return true;
}
if(a!=0){
result[i]="("+expb+"/"+expa+")";
number[i]=b/a;
if(pointsGame(n-1))
return true;
}
number[i]=a;
number[j]=b;
result[i]=expa;
result[j]=expb;
}
}
return false;
}
}
运行结果:true (((+)-)*)
相关文章推荐
- spring MVC详解(转)
- java回忆录(3)—ThreadLocal解决线程资源共享问题
- Elasticsearch Aggregation 多个字段分组统计 Java API实现
- javase知识点
- worldwind java导入栅格影像时的无效区域透明问题
- [疯狂Java]集合:Collections工具类、Enumeration(摒弃)
- Eclipse快捷
- java sql编辑器 动态报表 数据库备份还原 quartz定时任务调度 自定义表单 java图片爬虫
- [转]Elasticsearch Java API总汇
- Spring AOP 实现原理与 CGLIB 应用
- Java集合类框架—List、ArrayList、LinkedList
- spring hibernate框架问题
- Spring Boot 快速入门
- udp穿透简单讲解和实现(Java)
- Java传值
- Java 对象释放与 finalize 方法
- java报错Syntax error on token int, Dimensions expected after this token
- Error:(320, 57) java: -source 1.6 中不支持 diamond 运算符
- java中自定义时间减去某几天返回减去的天数时间
- spring 初始化 bean 或者销毁 bean 操作