题目:两个乒乓球队进行比赛,各出三人。
甲队为a,b,c三人,乙队为x,y,z三人。
已抽签决定比赛名单。
有人向队员打听比赛的名单。
a说他不和x比,
c说他不和x,z比,
请编程序找出三队赛手的名单
格式:
c ps y
import java.util.*; public class Test{ //HashMap 存储选手被选中状态来做 public static void main(String args[]){ char[] m ={'c','a','b'};//甲队-先按提供信息详细程度把他们排好(容易的先入手) char[] n ={'x','y','z'};//乙队 HashMap<Character,Integer> hm = new HashMap<Character,Integer>();//存储队员已有对手了吗 //初始都没有对手 hm.put('a',0); hm.put('b',0); hm.put('c',0); hm.put('x',0); hm.put('y',0); hm.put('z',0) ; for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ if(i==1&&j==0)continue; if(i==0&&(j==0||j==2))continue; if(hm.get(m[i])==0&&hm.get(n[j])==0){ //都没有对手就把他们设成对手 //他们的状态改为已有对手 hm.remove(m[i]); hm.put(m[i],1); hm.remove(n[j]); hm.put(n[j],1); System.out.println(m[i]+" ps "+n[j]); } } } } } /*---运行输出---- c ps y a ps z b ps x */