英文:
creating 3 pairs made from +6 objects where each pair has unique & unrepeated objects
问题
以下是您提供的内容的翻译部分:
我有一个可能有6个或更多对象的列表(+6个光子/伽玛射线),我需要从这个列表中选择3对对象(3个帕里中子)。这些配对需要都有唯一的对象,且每个伽玛射线在一个配对中只能使用一次。
例如,如果帕里中子1是由伽玛射线1(在代码中表示为ig1)和伽玛射线2(ig2)构成的,那么帕里中子2和帕里中子3必须由伽玛射线3、4、5、6等的所有可能组合构成(可能有超过6个伽玛射线,但我只关心总共3个帕里中子),依此类推。
我尝试使用以下方式嵌套了几个for循环:
(代码部分已省略)
这给我以下输出结果:
(输出部分已省略)
最初我认为这是正确的,直到我检查了n_g=6的情况,结果是错误的:
(输出部分已省略)
我尝试以以下方式找出6个伽玛射线的所有唯一可能配对:
(代码部分已省略)
这给我以下结果:
(输出部分已省略)
是否有任何想法,可以如何编辑第一个代码片段中的6个for循环,以获得3对帕里中子,其中每个伽玛射线只在1个帕里中子中使用,并且配对的顺序确实重要,这意味着如果一对是(0,1),则(1,0)是不同的,不可互换的。
英文:
I have a list of possible objects that could be 6 or more in length (+6 photons/gammas) and I need to make 3 pairs from this list (3 pions). These pairs need to all have unique objects, and no gamma can be re-used once it is in a pair.
For e.g. if pion1 is made from gamma1 (in the code: ig1) and gamma2 (ig2), then pions2 & pions3 have to be made from all possible combinations of gammas3,4,5,6,etc (it is possible to have more than 6 gammas, but I only care about 3 total pions), and so on.
I attempted to next several for loops in the following manner:
int n_g = 8; // n_g >= 6
boolean hasDVPi0P = false;
for (int ig1 = 0; ig1 < n_g-5 && !hasDVPi0P ; ig1++) {
for (int ig2 = ig1+1; ig2 < n_g-4 && !hasDVPi0P ; ig2++) {
for (int ig3 = ig2+1; ig3 < n_g-3 && !hasDVPi0P ; ig3++) {
for (int ig4 = ig3+1; ig4 < n_g-2 && !hasDVPi0P ; ig4++) {
for (int ig5 = ig4+1; ig5 < n_g-1 && !hasDVPi0P ; ig5++) {
for (int ig6 = ig5+1; ig6 < n_g && !hasDVPi0P ; ig6++) {
System.out.println("(" + ig1 + "," + ig2 + ") , (" + ig3 + "," + ig4 + ") , (" + ig5 + "," + ig6 + ")");
}
}
}
}
}
}
which gets me the following output:
(0,1) , (2,3) , (4,5)
(0,1) , (2,3) , (4,6)
(0,1) , (2,3) , (4,7)
(0,1) , (2,3) , (5,6)
(0,1) , (2,3) , (5,7)
(0,1) , (2,3) , (6,7)
(0,1) , (2,4) , (5,6)
(0,1) , (2,4) , (5,7)
(0,1) , (2,4) , (6,7)
(0,1) , (2,5) , (6,7)
(0,1) , (3,4) , (5,6)
(0,1) , (3,4) , (5,7)
(0,1) , (3,4) , (6,7)
(0,1) , (3,5) , (6,7)
(0,1) , (4,5) , (6,7)
(0,2) , (3,4) , (5,6)
(0,2) , (3,4) , (5,7)
(0,2) , (3,4) , (6,7)
(0,2) , (3,5) , (6,7)
(0,2) , (4,5) , (6,7)
(0,3) , (4,5) , (6,7)
(1,2) , (3,4) , (5,6)
(1,2) , (3,4) , (5,7)
(1,2) , (3,4) , (6,7)
(1,2) , (3,5) , (6,7)
(1,2) , (4,5) , (6,7)
(1,3) , (4,5) , (6,7)
(2,3) , (4,5) , (6,7)
Which I initially thought was correct until I checked n_g=6, which gets me
(0,1) , (2,3) , (4,5)
Which is clearly the wrong output. I tried to find all unique possible pairs for 6 gamma in the following way:
n_g=6;
for(int ig1 = 0; ig1 < n_g-1; ig1++) {
for(int ig2 = ig1+1; ig2 < n_g; ig2++) {
System.out.print("(" + ig1 + "," + ig2 + ") ");
}
System.out.println("");
}
which gives me
(0,1) (0,2) (0,3) (0,4) (0,5)
(1,2) (1,3) (1,4) (1,5)
(2,3) (2,4) (2,5)
(3,4) (3,5)
(4,5)
Does anyone have any ideas for how I would edit the first code segment with 6 for loops to get 3 pairs of pions where, if a gamma is used for 1 pion, it is not repeated again? Order in the pair does matter, and that means if a pair is (0,1) then pair (1,0) is different and not interchangeable.
答案1
得分: 1
我建议将此拆分为两个步骤:
- 找到所有成对的元素(顺序重要)。
- 找到所有成对组合(顺序无关)。
我编写了一个示例程序,供您分析。
首先,我创建了一个简单的Pair
类来跟踪成对的元素,比较值并打印它们:
public class Pair {
Integer one;
Integer two;
public Pair(Integer one, Integer two)
{
this.one = one;
this.two = two;
}
public boolean hasNoMatchingValues(Pair other)
{
return !this.one.equals(other.one) && !this.one.equals(other.two) && !this.two.equals(other.one) && !this.two.equals(other.two);
}
public static boolean hasNoMatchingValues(Pair...values)
{
for(int i = 0; i < values.length - 1; i++)
{
for(int j = i + 1; j < values.length; j++)
{
if(!values[i].hasNoMatchingValues(values[j]))
return false;
}
}
return true;
}
@Override
public String toString()
{
return "(" + one + ", " + two + ")";
}
}
在此之后,我使用以下代码来找到所有成对,并将它们匹配成3个一组的组合:
import java.util.ArrayList;
import java.util.List;
public class PairCombinations
{
public static void main(String[] args)
{
//构建唯一对象列表
List<Integer> objectList = new ArrayList<>();
for(int i = 0; i < 8; i++)
objectList.add(i);
//找到所有成对并添加到pairsList
List<Pair> pairsList = new ArrayList<>();
for(int i = 0; i < objectList.size() - 1; i++)
{
for(int j = i + 1; j < objectList.size(); j++)
{
pairsList.add(new Pair(objectList.get(i), objectList.get(j)));
pairsList.add(new Pair(objectList.get(j), objectList.get(i)));
}
}
//循环三次,因为您想要三个一组的组合。可以调整为递归形式。
List<List<Pair>> allCombos = new ArrayList<>();
for(int i = 0; i < pairsList.size() - 2; i++)
{
for(int j = i + 1; j < pairsList.size() - 1; j++)
{
for(int k = j + 1; k < pairsList.size(); k++)
{
Pair one = pairsList.get(i), two = pairsList.get(j), three = pairsList.get(k);
if(Pair.hasNoMatchingValues(one, two, three))
{
List<Pair> combos = new ArrayList<>();
combos.add(one);
combos.add(two);
combos.add(three);
allCombos.add(combos);
}
}
}
}
//打印结果
for(List<Pair> combo : allCombos)
System.out.println(combo);
}
}
英文:
I would recommend breaking this down into two steps.
- Find all pairs of elements (where order matters).
- Find all combinations of pairs (where order does not matter).
I put together a example program if you want to analyze it.
First thing I did was make a simple Pair
class to track the pairs, compare values, and print them:
public class Pair {
Integer one;
Integer two;
public Pair(Integer one, Integer two)
{
this.one = one;
this.two = two;
}
public boolean hasNoMatchingValues(Pair other)
{
return !this.one.equals(other.one) && !this.one.equals(other.two) && !this.two.equals(other.one) && !this.two.equals(other.two);
}
public static boolean hasNoMatchingValues(Pair...values)
{
for(int i = 0; i < values.length - 1; i++)
{
for(int j = i + 1; j < values.length; j++)
{
if(!values[i].hasNoMatchingValues(values[j]))
return false;
}
}
return true;
}
@Override
public String toString()
{
return "(" + one + ", " + two + ")";
}
}
After this, I use the following code to find all pairs and then match them up into there combinations of 3:
import java.util.ArrayList;
import java.util.List;
public class PairCombinations
{
public static void main(String[] args)
{
//Builds the list of unique objects
List<Integer> objectList = new ArrayList<>();
for(int i = 0; i < 8; i++)
objectList.add(i);
//Finds all pairs and adds them to pairsList
List<Pair> pairsList = new ArrayList<>();
for(int i = 0; i < objectList.size() - 1; i++)
{
for(int j = i + 1; j < objectList.size(); j++)
{
pairsList.add(new Pair(objectList.get(i), objectList.get(j)));
pairsList.add(new Pair(objectList.get(j), objectList.get(i)));
}
}
//Loop three times since you want three combinations. Can be adjusted to a recursive form.
List<List<Pair>> allCombos = new ArrayList<>();
for(int i = 0; i < pairsList.size() - 2; i++)
{
for(int j = i + 1; j < pairsList.size() - 1; j++)
{
for(int k = j + 1; k < pairsList.size(); k++)
{
Pair one = pairsList.get(i), two = pairsList.get(j), three = pairsList.get(k);
if(Pair.hasNoMatchingValues(one, two, three))
{
List<Pair> combos = new ArrayList<>();
combos.add(one);
combos.add(two);
combos.add(three);
allCombos.add(combos);
}
}
}
}
//Print the results
for(List<Pair> combo : allCombos)
System.out.println(combo);
}
}
Please let me know if you have any questions.
答案2
得分: 0
以下是翻译好的内容:
int n_g = 6;
boolean hasDVPi0P = false;
for (int ig1 = 0; ig1 < n_g && !hasDVPi0P ; ig1++) {
for (int ig2 = 0; ig2 < n_g && !hasDVPi0P ; ig2++) {
for (int ig3 = 0; ig3 < n_g && !hasDVPi0P ; ig3++) {
for (int ig4 = 0; ig4 < n_g && !hasDVPi0P ; ig4++) {
for (int ig5 = 0; ig5 < n_g && !hasDVPi0P ; ig5++) {
for (int ig6 = 0; ig6 < n_g && !hasDVPi0P ; ig6++) {
if (true
&& ig1 < ig2 && ig3 < ig4 && ig5 < ig6 // 不同光子组成的每对
&& ig1 < ig3 && ig1 < ig5 && ig3 < ig5 // 每对中第一个光子唯一
&& ig2 != ig3 && ig2 != ig4 && ig2 != ig5 && ig2 != ig6 // 清除其他配对
&& ig3 != ig4 && ig3 != ig5 && ig3 != ig6
&& ig4 != ig5 && ig4 != ig6
) {
System.out.println("(" + ig1 + "," + ig2 + ") , (" + ig3 + "," + ig4 + ") , (" + ig5 + "," + ig6 + ")\n");
}
}
}
}
}
}
}
你可以随意修改 int n_g = 6
,但此方法保持了嵌套的 for 循环,并使用了 if 语句处理配对,可以根据需要修改条件。对我来说,这种方法非常有用,因为另一个答案虽然内容广泛,但创建了完全不同的类。我需要保持对三个配对的检查相对简洁,比在另一个答案中提供的内容要短。
英文:
In case anyone wanted to see a version with the nested for loops, and a version that considers (0,1) the same as (1,0). This is a slightly simpler version than the answer with the two classes, and I thought someone might benefit if they ever came around looking for a way to make pairs in a slightly different way.
int n_g = 6;
boolean hasDVPi0P = false;
for (int ig1 = 0; ig1 < n_g && !hasDVPi0P ; ig1++) {
for (int ig2 = 0; ig2 < n_g && !hasDVPi0P ; ig2++) {
for (int ig3 = 0; ig3 < n_g && !hasDVPi0P ; ig3++) {
for (int ig4 = 0; ig4 < n_g && !hasDVPi0P ; ig4++) {
for (int ig5 = 0; ig5 < n_g && !hasDVPi0P ; ig5++) {
for (int ig6 = 0; ig6 < n_g && !hasDVPi0P ; ig6++) {
if( true
&& ig1<ig2 && ig3<ig4 && ig5<ig6 // different photons in each pair
&& ig1<ig3 && ig1<ig5 && ig3<ig5 // unique first photon from each pair
&& ig2!=ig3 && ig2!=ig4 && ig2!=ig5 && ig2!=ig6 // clean up rest of pairs below
&& ig3!=ig4 && ig3!=ig5 && ig3!=ig6
&& ig4!=ig5 && ig4!=ig6
){
System.out.println("(" + ig1 + "," + ig2 + ") , (" + ig3 + "," + ig4 + ") , (" + ig5 + "," + ig6 + ")\n");
}
}
}
}
}
}
}
You can, of course, change int n_g = 6
, but this method keeps the nested for loops and uses an if statement to deal with the pairs, which can be modified for whatever conditions you may need.
This was, ultimately, the most useful for me because the other answer, while extensive, created entirely separate classes. I needed to keep the check for 3 pairs fairly contained and shorter than what was provided in the other answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论