首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java:数组的可能组合

Java:数组的可能组合
EN

Stack Overflow用户
提问于 2018-09-30 07:06:10
回答 1查看 79关注 0票数 0

我正在解决一个java问题:

问题:

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

示例:

输入:(int list) l= 3,1,4,1

输出:(int) 4311

输入:(int list) l= 3,1,4,1,5,9

输出:(int) 94311

--

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

这是我的代码:

代码语言:javascript
复制
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";
	}

}

有人能让它变得更有效率吗?我试过了,但没能做到。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-09-30 09:27:43

您只需要按数字的降序对数组进行排序,并将它们写在一行中。这能解决问题吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52573261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档