gpt4 book ai didi

Java:数组的可能组合

转载 作者:行者123 更新时间:2023-12-02 10:45:58 25 4
gpt4 key购买 nike

我正在解决一个java问题:

问题:

用户输入一个整数数组,该数组的大小可以是1-9。我需要重新排列数组,以便它打印可被 3 整除的最大值。我不需要使用数组中的所有整数。

示例:

输入: (整数列表)l = [3, 1, 4, 1]

输出: (整数)4311

输入: (整数列表)l = [3, 1, 4, 1, 5, 9]

输出: (整数)94311

--

到目前为止我花了大约 5 个小时。我的代码可以工作,但它几乎会尝试每种组合,直到其中一种组合有效为止。我需要更高效的代码。

这是我的代码:

public class CodedMessage {
public static int zero = 0;
public static int one = 0;
public static int two = 0;
public static int three = 0;
public static int four = 0;
public static int five = 0;
public static int six = 0;
public static int seven = 0;
public static int eight = 0;
public static int nine = 0;

public static int best = 0;
public static int current = 0;

public static int newZero = 0;
public static int newOne = 0;
public static int newTwo = 0;
public static int newThree = 0;
public static int newFour = 0;
public static int newFive = 0;
public static int newSix = 0;
public static int newSeven = 0;
public static int newEight = 0;
public static int newNine = 0;

public static void main(String[] args) {
int[] myIntArray = {3,1,4,1};

System.out.println(answer(myIntArray));
}

public static int answer(int[] l) {
String line ="";
for (int c : l) {
line += c;
}
zero = line.length() - line.replace("0", "").length();
one = line.length() - line.replace("1", "").length();
;
two = line.length() - line.replace("2", "").length();
;
three = line.length() - line.replace("3", "").length();
;
four = line.length() - line.replace("4", "").length();
;
five = line.length() - line.replace("5", "").length();
;
six = line.length() - line.replace("6", "").length();
;
seven = line.length() - line.replace("7", "").length();
;
eight = line.length() - line.replace("8", "").length();
;
nine = line.length() - line.replace("9", "").length();
;
if (Integer.parseInt(line)%3 != 0) {

}else {
possibleStrings(l.length, l, "");
}



return best;

}

public static String possibleStrings(int maxLength, int[] alphabet, String curr) {


if (!curr.equals("")) {
current = Integer.parseInt(curr);

}

if (current > best) {
if (current % 3 == 0) {
String line = Integer.toString(current);
newZero = line.length() - line.replace("0", "").length();
newOne = line.length() - line.replace("1", "").length();
;
newTwo = line.length() - line.replace("2", "").length();
;
newThree = line.length() - line.replace("3", "").length();
;
newFour = line.length() - line.replace("4", "").length();
;
newFive = line.length() - line.replace("5", "").length();
;
newSix = line.length() - line.replace("6", "").length();
;
newSeven = line.length() - line.replace("7", "").length();
;
newEight = line.length() - line.replace("8", "").length();
;
newNine = line.length() - line.replace("9", "").length();
;

if (zero >= newZero && one >= newOne && two >= newTwo && three >= newThree && four >= newFour
&& five >= newFive && six >= newSix && seven >= newSeven && eight >= newEight
&& nine >= newNine) {
best = current;
}
}

}

if (curr.length() == maxLength) {
if (!curr.equals("") &&Integer.parseInt(curr)%3 != 0) {
return "hi";
}
} else {
for (int i = 0; i < alphabet.length; i++) {
String oldCurr = curr;
curr += alphabet[i];

possibleStrings(maxLength, alphabet, curr);
curr = oldCurr;
}
}
return "hi";
}

}

有人可以提高效率吗,我尝试过但没能做到。

谢谢!

最佳答案

一个数要能被3整除,它的各位数字之和必须能被3整除。您可以像这样进行:

  1. 对数字进行排序。

  2. 使用已排序的数字来查找具有最大数字的组数字。为此,首先检查总和是否能被 3 整除。然后尝试使用 n-1 个最大的数字,依此类推。您找到的第一个组就是您要查找的内容

编辑:根据评论中的建议对于第二步。查找k=sum%3;

if(k==0) 你就有了解决方案。

如果不是,则从最低位(数组末尾)开始检查所有数字if digital%3=k。如果是这样,请将其删除并找到解决方案。如果没有数字可以做到这一点,请尝试使用 2 位数字、3 位数字,依此类推。

关于Java:数组的可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52573261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com