用JAVA如何算出24点?

Python017

用JAVA如何算出24点?,第1张

24点的源代码,因该可以计算出4则运算24 public class Test24Point{ public static void main(String[] args){ int index = 0 int temp = 0 int totalSUC = 0 int numb[] = new int[4]//the first four numbers double num[][] = new double[36][3]//three numbers after calculating double total[] = new double[6]//the number after three steps of calculating double p[][] = new double[6][8] double q[][] = new double[3][7]//System.out.println(2465%108) //System.out.println(2465/108) System.out.println("\"a--b\"means\"b-a\"") System.out.println("\"a//b\"means\"b/a\"\n") /* for(int h = 0h <= 9h ++)//Get the first four numbers for calculating and store into the array numb[4]for(int i = 0i <= 9i ++) for(int j = 0j <= 9j ++) for(int k = 0k <= 9k ++){ numb[0] = h numb[1] = i numb[2] = j numb[3] = k }*/ for(int i = 0 i <4 i ++){ numb = Integer.parseInt(args) } for(int i = 0i <3i ++)//Get two of the four to calculate and then store the new number into the array p for(int j = i + 1j <4 j ++,temp ++){ p[temp][0] = numb + numb[j] p[temp][1] = numb - numb[j] p[temp][2] = numb[j] - numb p[temp][3] = numb * numb[j]if(numb[j] != 0) p[temp][4] = numb / (double)numb[j] else p[temp][4] = 10000 if(numb != 0) p[temp][5] = numb[j] / (double)numb else p[temp][5] = 10000

粗略看了下,至少发现三个BUG:

1.没有考虑int类型相除时是否整除的问题,如3/4在你的算法中直接以零作为计算结果

2.如果前两个或前三个数计算结果已经为24,则直接输出,没有等到四个全部参与计算;

3.类似n == 24.0这种浮点数的相等判断在绝大多数计算机语言中都是不正确的。