Java程序,用于查找一副牌中四张牌的组合数等于24
我有一个Java程序,它将获得一副牌,并计算出四张牌的组合数等于24。到目前为止,我有:Java程序,用于查找一副牌中四张牌的组合数等于24,java,Java,我有一个Java程序,它将获得一副牌,并计算出四张牌的组合数等于24。到目前为止,我有: public class Assign5b { public static void main(String[] args) { int[] deck = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1,
public class Assign5b {
public static void main(String[] args) {
int[] deck = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int total;
total = calculate(deck);
output(total);
}
public static int calculate(int[] deck) {
int total = 0;
int stack1, stack2, stack3, stack4, accumulate;
for (stack1 = 0; stack1 < 52; stack1++) {
for (stack2 = (stack1 + 1); stack2 < 52; stack2++) {
for (stack3 = (stack2 + 1); stack3 < 52; stack3++) {
for (stack4 = (stack3 + 1); stack4 < 52; stack4++) {
accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
if (accumulate == 24)
total++;
}
}
}
}
return total;
}
public static void output(int total){
System.out.println ("The total number of card combinations of 4 that \n" + "equal 24 is: " + total);
}
}
公共类分配5b{
公共静态void main(字符串[]args){
int[]deck={1,2,3,4,5,6,7,8,9,10,11,12,13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
整数合计;
总计=计算(甲板);
产出(总数);
}
公共静态整数计算(整数[]甲板){
int-total=0;
int stack1,stack2,stack3,stack4,累加;
用于(stack1=0;stack1<52;stack1++){
对于(stack2=(stack1+1);stack2<52;stack2++){
对于(stack3=(stack2+1);stack3<52;stack3++){
对于(stack4=(stack3+1);stack4<52;stack4++){
累计=(甲板[stack1]+甲板[stack2]+甲板[stack3]+甲板[stack4]);
如果(累计==24)
总计++;
}
}
}
}
返回总数;
}
公共静态无效输出(整数总计){
System.out.println(“4个“+”等于24的卡组合的总数为:“+总数”);
}
}
所以我的问题不是它没有运行,或者它没有显示正确的值,而是那些嵌套的循环。我不想让他们在那里。这使得它的运行效率非常低,我知道有更好的方法让它运行,但我无法想象它。您可以在每个循环的开头添加检查,看看运行总数是否已经超过24。在这种情况下,无论如何执行这些循环都没有意义:
public static int calculate(int[] deck, int target) {
int total = 0;
int stack1, stack2, stack3, stack4, accumulate;
for (stack1 = 0; stack1 < 52; stack1++) {
if (deck[stack1] > target) break;
for (stack2 = (stack1 + 1); stack2 < 52; stack2++) {
if (deck[stack1] + deck[stack2] > target) break;
for (stack3 = (stack2 + 1); stack3 < 52; stack3++) {
if (deck[stack1] + deck[stack2] + deck[stack3] > target) break;
for (stack4 = (stack3 + 1); stack4 < 52; stack4++) {
if (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4] > target) break;
accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
if (accumulate == 24)
total++;
}
}
}
}
return total;
}
您可以在每个循环的开头添加检查,以查看运行总数是否已经超过24。在这种情况下,无论如何执行这些循环都没有意义:
public static int calculate(int[] deck, int target) {
int total = 0;
int stack1, stack2, stack3, stack4, accumulate;
for (stack1 = 0; stack1 < 52; stack1++) {
if (deck[stack1] > target) break;
for (stack2 = (stack1 + 1); stack2 < 52; stack2++) {
if (deck[stack1] + deck[stack2] > target) break;
for (stack3 = (stack2 + 1); stack3 < 52; stack3++) {
if (deck[stack1] + deck[stack2] + deck[stack3] > target) break;
for (stack4 = (stack3 + 1); stack4 < 52; stack4++) {
if (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4] > target) break;
accumulate = (deck[stack1] + deck[stack2] + deck[stack3] + deck[stack4]);
if (accumulate == 24)
total++;
}
}
}
}
return total;
}
我不明白你的推理。目标初始化为什么?目标将初始化为24。但是,除非您以升序重新排列数组,否则这将不起作用。它将导致循环在数组变为1之前中断。@jacob参数
target
是您想要的总数,例如24。另外,当您说不需要循环时,您的意思是什么?我真的不知道怎么做。在这一点上,显然你可以告诉我这是为了一个作业,但我已经提交了我的工作代码,所以我的成绩被锁定了,我只是想通过去掉嵌套循环来让它运行得更好。@WillKaczmarek是的,我要加一条说明,这假设甲板是按升序排序的。对一个小数组进行排序非常简单,并且不会影响总体运行时间。我不理解你的推理。目标初始化为什么?目标将初始化为24。但是,除非您以升序重新排列数组,否则这将不起作用。它将导致循环在数组变为1之前中断。@jacob参数target
是您想要的总数,例如24。另外,当您说不需要循环时,您的意思是什么?我真的不知道怎么做。在这一点上,显然你可以告诉我这是为了一个作业,但我已经提交了我的工作代码,所以我的成绩被锁定了,我只是想通过去掉嵌套循环来让它运行得更好。@WillKaczmarek是的,我要加一条说明,这假设甲板是按升序排序的。对一个小数组进行排序非常简单,不会影响总体运行时间。