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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 廉伟 中级黑马   /  2012-9-10 08:39  /  1190 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

评分

参与人数 1黑马币 +30 收起 理由
王德升 + 30 赞一个!

查看全部评分

3 个回复

倒序浏览

回帖奖励 +10

本帖最后由 周磊 于 2012-9-10 09:12 编辑

class AA
{
         
         //如果这儿字符串比较长可以用递归得出全排列,这儿由于只有三个字符,所以直接得出
         static String[] m={"xyz","xzy","yzx","yxz","zxy","zyx"};
         
         //进行条件判断
         static void f(){
                 for(int i = 0;i<m.length;i++){
                         if(m.toCharArray()[0]!='x'){
                                 if(m.toCharArray()[2]!='x' && m.toCharArray()[2]!='z')
                                         System.out.println("abc对应的选手为:"+m);
                         }
                 }
         }
         
         
         public static void main(String[] args) {
                 
                 f();
                 
        }
}
程序结果:abc对应的选手为:zxy
这种题的思路就是先把所有的可能性先得出,再由条件进行筛除,最终得到最后的答案,如果是可能性比较多的情况,会使用递归的方法
回复 使用道具 举报

回帖奖励 +10

用循环出来

public class BiSaiNing {
        public static void main(String[] args) {                for(int a=120;a<123;a++)                {                        for(int b=120;b<123;b++)                        {                                for(int c=120;c<123;c++)                                {                                        if(a!=120&&c!=122&&c!=120&&a!=b&&a!=c&&b!=c)                                        {                                                char m=(char)a;                                                char n=(char)b;                                                char q=(char)c;                                                System.out.println("a对手是:"+m+"\tb对手是:"+n+"\tc对手是:"+q);                                                                                        }                                }                        }                }        }        }

回复 使用道具 举报
  1. import java.util.HashSet;

  2. public class PingPong {
  3.        
  4.         /**
  5.          * @param args
  6.          */
  7.         public static void main(String[] args) {
  8.                 char[] b = { 'x', 'y', 'z' };
  9.                 HashSet<String> set = new HashSet<String>();
  10.                 perm(b, 0, set);
  11.                 for (String s : set) {
  12.                         if (s.length() >= 3) {
  13.                                 if (s.charAt(0) != 'x') {
  14.                                         if (s.charAt(2) != 'x' && s.charAt(2) != 'z') {
  15.                                                 System.out.println("abc对应的选手是:" + s);
  16.                                                 return;
  17.                                         }
  18.                                 }
  19.                         }
  20.                 }
  21.         }
  22.        
  23.         static void swapArrayElements(char a[], int lhs, int rhs) {
  24.                 char temp = a[lhs];
  25.                 a[lhs] = a[rhs];
  26.                 a[rhs] = temp;
  27.         }
  28.        
  29.         /**
  30.          * 字符数组全排列
  31.          *
  32.          * @param a
  33.          * @param start
  34.          * @param set
  35.          */
  36.         static void perm(char a[], int start, HashSet<String> set) {
  37.                 if (start == a.length - 1) {
  38.                         // 输出排列结果
  39.                         set.add(new String(a));
  40.                         return;
  41.                 }
  42.                 else {
  43.                         for (int i = start; i <= a.length - 1; i++) {
  44.                                 // 将数组片段的各元素与首元素交换
  45.                                 swapArrayElements(a, start, i);
  46.                                 // 对交换后的,去掉首元素的数组片段进行全排列
  47.                                 perm(a, start + 1, set);
  48.                                 // 交换回来
  49.                                 swapArrayElements(a, start, i);
  50.                         }
  51.                 }
  52.         }
  53. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马