数学24场比赛的Java [英] Math twenty four game Java

查看:208
本文介绍了数学24场比赛的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

24游戏是一个算术游戏,其目标是找到一种方法来操作四个整数,使得最终的结果是24。加法,减法,乘法,除法或在数字的任何命令可以被用于使四位数字运算从一到九等于24

规则很简单:你必须使用每个号码只有一次,并且只有在用户宣读找到一个方程的4个数字,获得24

例如,对于数字4,7,8,8,一个可能的解决方案是:(7-(8/8))* 4 = 24

。 例如输入:2,2,4和7可以采用多种方式,得到24

最套4位可以在导致24的多个方程可以使用

2 + 2 *(4 + 7)= 24

2 + 2 *(7 + 4)= 24

(2 + 2)* 7-4 = 24

(2 * 2)* 7-4 = 24

2 *(2 * 7)-4 = 24

有也4个数不能导致任何入方程等于与24。例如1,1,1,1组合。在这种情况下,你的程序应该返回,没有任何可能的公式等于24。

请注意:虽然我们将进入1和9之间的4个整数,我们将用双打来计算所有的操作。例如,数字3,3,8,8可以组合成下式:8 /(3-8 / 3)= 24

工作流:你的程序应该从用户和输出读出的4个数字的公式,其导致24.算法应列举的4号,所有可能的组合和所有可能的公式的所有可能的顺序。没有为这个项目我需要将洗牌经营了这么4运营商和3各公式中所使用的所有64种可能的组合,并且在方程也占括号的方法帮助任何需要的GUI。我不知道从哪里开始。

解决方案

如果您可以生成一个字符串的排列。你需要做的是让所有人都获得所有可能的这些数字的排列的数字。

现在你只需要插件运营商的排列(3在同一时间)。

有关可以生成经营者所有排列,并将它们存储在一个阵列中,因为这将保持不变每一种情况。而产生出每个排列的,随便挑的前3个字符,因为我们正在寻找的3组出了4个可能的。

一旦你的,它只是读取数字的排列,然后读取运营商的置换和评估前pression的问题。

有关的参考,我做了一个简单的 演示函数,发现排列一个字符串中的Java。递归函数看起来类似(从<一个href="http://stackoverflow.com/questions/361/generate-list-of-all-possible-permutations-of-a-string">relevant SO后):

 公共无效PERMUT(字符串STR1,字符串STR2){
   如果(str2.length()!= 0){
     炭CH = str2.charAt(0);
     的for(int i = 0; I&LT; = str1.length();我++)
        PERMUT(str1.substring(0,i)的+ CH + str1.substring(ⅰ,str1.length()),
                 str2.substring(1,str2.length()));
  }其他{
    的System.out.println(STR1);
  }
 }
 

如果你能成功地生成一个字符串的所有排列,上面的工作应该是可行的。我希望它可以让你在正确的方向开始。

The 24 Game is an arithmetical game in which the objective is to find a way to manipulate four integers so that the end result is 24. Addition, subtraction, multiplication, or division in any order of the numbers may be used to make the four digits operations from one to nine equal 24.

The rules are simple: you have to use each number only once and only the 4 numbers that were read from the user to find one equation to obtain 24.

For example, for the numbers 4,7,8,8, a possible solution is: (7-(8/8))*4=24.

Most sets of 4 digits can be used in multiple equations that result in 24: for example the input: 2, 2, 4 and 7 can be used in multiple ways to obtain 24:

2+2*(4+7) = 24

2+2*(7+4) = 24

(2+2)*7-4 = 24

(2*2)*7-4 = 24

2*(2*7)-4 = 24

There are also combinations of 4 numbers that cannot result into any equation equal with 24. For example 1,1,1,1. In this case, your program should return that there is no possible equation equal with 24.

Note: Although we will enter 4 integers between 1 and 9, we will use doubles to compute all the operations. For example, the numbers 3,3,8,8 can be combined into the formula: 8/(3-8/3) = 24.

Workflow: Your program should read the 4 numbers from the user and output a formula that results in 24. The algorithm should enumerate all the possible orders of 4 numbers, all the possible combinations and all the possible formulas. There is no required GUI for this project I need help with a method that will shuffle the operators for all 64 possible combos so 4 operators and 3 being used in each equation and also account for parenthesis during the equations . I have no idea where to begin.

解决方案

If you can generate permutations of a string. You need to do that for all the numbers to get all the permutations possible for those numbers.

Now you just need to plugin permutations of operators (3 at a time).

For that you can generate all the permutations of the operators and store them in an array, as that would remain constant for each case. And out of each permutation generated, just pick the first 3 characters as we are looking at groups of 3 out of the 4 possible.

Once you have that, it's simply a matter of reading a permutation of the numbers and then reading a permutation of operators and evaluating the expression.

For reference, I have made a simple demo of a function that finds permutations of a string in Java. The recursive function looks something like (from a relevant SO Post):

 public void permut(String str1,String str2){
   if(str2.length() != 0){
     char ch = str2.charAt(0);
     for(int i = 0; i <= str1.length();i++)
        permut(str1.substring(0,i) + ch + str1.substring(i,str1.length()),
                 str2.substring(1,str2.length()));
  }else{
    System.out.println(str1);
  }
 }

If you can successfully generate all permutations of a string, the above exercise should be doable. I hope it gets you started in the right direction.

这篇关于数学24场比赛的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆