java经典算法_017两个乒乓球队进行比赛,找出比赛对手名单。

时间:2022-03-28 11:26:56
package wzs.arithmetics;

import java.util.ArrayList;
import java.util.List;

//题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。
//已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
public class Test_wzs17
{

public static void main(String[] args)
{
// 甲队成员排序,固定
Pingpang team1Member = new Pingpang("a", "b", "c");
System.out.println("甲队:" + team1Member);
// 乙队成员
String[] team2Member =
{
"x", "y", "z"
};
// 乙队所有的排序可能
List<Pingpang> team2List = new ArrayList<Pingpang>();
// 乙队临时排序
Pingpang tempTemp;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
tempTemp = new Pingpang(team2Member[i], team2Member[j], team2Member[k]);
// 球队队员不重复
if (!tempTemp.member1.equals(tempTemp.member2) && !tempTemp.member2.equals(tempTemp.member3))
{
// a说他不和x比,c说他不和x,z比
if (!tempTemp.member1.equals("x") && !tempTemp.member3.equals("x") && !tempTemp.member3.equals("z"))
{
// System.out.println("乙队:" + tempTemp);
team2List.add(tempTemp);
}
}
}
}
}
for (Pingpang pingpang : team2List)
{
System.out.println("乙球:" + pingpang);
}
}
}

/**
* 乒乓球队
* @author wWX154783
*
*/
class Pingpang
{
String member1;// 成员1
String member2;// 成员2
String member3;// 成员3

public Pingpang()
{
}

public Pingpang(String member1, String member2, String member3)
{
this.member1 = member1;
this.member2 = member2;
this.member3 = member3;
}

@Override
public String toString()
{
return "球队成员{" + member1 + "," + member2 + "," + member3 + "}";
}

}