A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© kakasa 中级黑马   /  2014-10-12 21:19  /  1631 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
从题目条件可以知道,赛程表应该是:
a-z
b-x
c-y
怎么用代码实现输出赛程表?

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 淡定

查看全部评分

4 个回复

倒序浏览
  1. import java.util.*;
  2. class RaceCard
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 StringBuilder sb1=new StringBuilder("abc");
  7.                 StringBuilder sb2=new StringBuilder("xyz");
  8.                
  9.                 method(sb1,sb2);
  10.                
  11.                
  12.         }
  13.         public static void method(StringBuilder sb1,StringBuilder sb2)
  14.         {
  15.                 ArrayList al=new ArrayList();
  16.                                         //a1用于存储题干中已知的错误组合。
  17.                 ArrayList a2=new ArrayList();
  18.                                         //a2用于存储经while循环判断之后得到的赛程表。
  19.                 al.add("ax");
  20.                 al.add("cx");
  21.                 al.add("cz");
  22.                




  23.                 while (sb1.length()!=0)
  24.                         //反复遍历sb1,sb2每有一种组合符合要求,就在sb1,sb2中删掉相应的元素,
  25.                         //直到找到所有符合题意的组合,即sb1,sb2都为空。
  26.                        
  27.                 {                               
  28.                         for (int i=0;i<sb1.length() ;i++ )
  29.                         {
  30.                                 String temp=null;
  31.                                 int count =0,index=0;
  32.                                                
  33.                                 for (int j=0; j<sb2.length();j++ )
  34.                                        
  35.                                 {
  36.                                         if(!al.contains(sb1.charAt(i)+""+sb2.charAt(j)))
  37.                                                
  38.                                         {
  39.                                                 temp=sb1.charAt(i)+"-"+sb2.charAt(j);
  40.                                                 count++;
  41.                                                 index=j;
  42.                                         }
  43.                                        
  44.                                 }//在满足已知条件下,判断sb1中一个元素与sb2中某一元素组合的可能,用count记录可能组合数
  45.                                  //就像ay,az都不在集合a1中,此时count==2。当循环到c时,只有cy符合要求了,此时count==1.
  46.                                  //也就是说,当且仅当count==1的时候,该组合才是所求一种组合。
  47.                                 if(count==1)
  48.                                 {
  49.                                         a2.add(temp);
  50.                                         sb1.deleteCharAt(i);
  51.                                         sb2.deleteCharAt(index);

  52.                                 }
  53.                                        

  54.                         }
  55.                 }



  56.                 String[] str=(String[])a2.toArray(new String[a2.size()]);
  57.                                 //将获取到的组合都存入a2中,并将a2转换为一个String数组。
  58.                 Arrays.sort(str);
  59.                                 //将该数组按自然顺序排序。
  60.                 System.out.println("甲乙两队的赛程表为:");
  61.                 for(int i=0;i<str.length;i++)  
  62.             System.out.println(str[i]);  
  63.                
  64.        
  65.         }
  66. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 很给力!

查看全部评分

回复 使用道具 举报
牛,这考的..全是思想啊...
回复 使用道具 举报
看懂。给2L 赞一个
回复 使用道具 举报

赞一个,!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马