英文:
I'm trying to get combinations but I don't know how to do it
问题
在一个测试用例中,我的输入是1、2、3、4,我想要得到:
{1+2, 3+4}
{1+3, 2+4}
{1+4, 2+3}
但我只能得到{1+2, 3+4},我想不出任何方法来得到{1+3, 2+4}。我需要一些算法建议,想不出任何条件来做到这一点。
for (int j=0; j<woodLength.length; j++) {
if (woodLength[j][2] != 1) {
// 一个可以使用木块创建木板的函数
for (int i=0; i<woodLength.length; i++) {
// 将已使用的木板设置为1,未使用的设置为0
if (woodLength[i][1] != 1) {
woodenBoardHeight.add(woodLength[i][0]+woodLength[i+1][0]);
System.out.println(woodenBoardHeight.get(wBHCount));
wBHCount++;
woodLength[i][1] = 1;
woodLength[i+1][1] = 1;
}
}
for (int i=0; i<woodLength.length; i++) {
woodLength[i][1] = 0;
}
woodLength[j][2] = 1;
woodLength[j+1][2] = 1;
}
}
这是我的代码,目前这会输出{3,7},但我还想要{4,6}和{5,5}。
如果有帮助的话,我正在尝试解决CCC 2017年高级部分第3题。
链接:https://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/seniorEF.pdf
英文:
In a test case, I have 1 2 3 4 as the inputs, I want to get
{1+2, 3+4}
{1+3, 2+4}
{1+4, 2+3}
but I only can do {1+2, 3+4} I can't think of any ways to get {1+3, 2+4}.
I need some algorithm advice, can't think of any conditions to do this.
for (int j=0; j<woodLength.length; j++) {
if (woodLength[j][2] != 1) {
// a function that can create the boards using the wood pieces
for (int i=0; i<woodLength.length; i++) {
// set used boards as 1 and unused as 0
if (woodLength[i][1] != 1) {
woodenBoardHeight.add(woodLength[i][0]+woodLength[i+1][0]);
System.out.println(woodenBoardHeight.get(wBHCount));
wBHCount ++;
woodLength[i][1] = 1;
woodLength[i+1][1] = 1;
}
}
for (int i=0; i<woodLength.length; i++) {
woodLength[i][1] = 0;
}
woodLength[j][2] = 1;
woodLength[j+1][2] = 1;
}
}
this is my code, right now this prints {3,7} but I also want {4,6} and {5,5}
If it's helpful, I'm trying to solve CCC 2017 Senior S3 question
https://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/seniorEF.pdf
答案1
得分: 3
一个用于生成数组所有组合的算法利用了无符号二进制计数器。
数组:1 2 3 4
0 0 0 0 [] 空组合
0 0 0 1 [4]
0 0 1 0 [3]
0 0 1 1 [3, 4]
以此类推。
在您的情况下,您想要包含两个数字和两个数字的组合。我们可以通过测试二进制计数器,并在二进制计数器恰好有2位打开时创建组合来实现这一点。
数组:1 2 3 4
0 0 1 1 [3, 4] & [1, 2]
0 1 0 1 [2, 4] & [1, 3]
0 1 1 0 [2, 3] & [1, 4]
1 0 0 1 [1, 4] & [2, 3]
1 0 1 0 [1, 3] & [2, 4]
1 1 0 0 [1, 2] & [3, 4]
在您的示例中,配对的顺序不重要。您可以消除重复的配对,从而得到您要寻找的三种情况。
英文:
One algorithm to create all the combinations of an array makes use of an unsigned binary counter.
Array: 1 2 3 4
0 0 0 0 [] empty combination
0 0 0 1 [4]
0 0 1 0 [3]
0 0 1 1 [3, 4]
and so on.
In your case, you want the combinations that are 2 digits plus 2 digits. We can achieve this by testing the binary counter and creating the combination when the binary counter has exactly 2 bits on.
Array: 1 2 3 4
0 0 1 1 [3, 4] & [1, 2]
0 1 0 1 [2, 4] & [1, 3]
0 1 1 0 [2, 3] & [1, 4]
1 0 0 1 [1, 4] & [2, 3]
1 0 1 0 [1, 3] & [2, 4]
1 1 0 0 [1, 2] & [3, 4]
In your example, the order of the pairs is not important. You can eliminate the duplicate pairs, and you have the three conditions you're looking for.
答案2
得分: -1
以下是翻译好的内容:
使用以下代码可以得到预期的解决方案:
public static void main(String[] args) {
List<Integer> num;
Integer[] numArray = {1, 2, 3, 4};
num = Arrays.asList(numArray);
List<Integer> newList = new ArrayList<>();
for (int i = 1; i < num.size(); i++) {
int[] intArray = new int[2];
newList.addAll(num);
newList.remove(0);
newList.remove(i - 1);
intArray[0] = num.get(0) + num.get(i);
intArray[1] = newList.get(0) + newList.get(1);
System.out.println("{" + intArray[0] + "," + intArray[1] + "}");
}
}
结果将会是这样的:
{3,7}
{4,6}
{5,5}
英文:
Use this code to get the solution as expected
public static void main(String[] args) {
List<Integer> num;
Integer[] numArray = {1,2,3,4};
num = Arrays.asList (numArray);
List<Integer> newList = new ArrayList<> ();
for(int i=1;i<num.size ();i++) {
int[] intArray = new int[2];
newList.addAll (num);
newList.remove (0);
newList.remove (i-1);
intArray[0]=num.get (0)+num.get (i);
intArray[1] = newList.get (0)+newList.get (1);
System.out.println ("{"+intArray[0]+","+intArray[1]+"}");
}
}
The Result will be like this :
{3,7}
{4,6}
{5,5}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论