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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 任睦强 高级黑马   /  2012-5-17 23:46  /  1877 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

A,B,C,D 4个美女各有 5,6,7,8件衣服。现在要将自己的衣服交换给别人穿。
但是本人手中不能有自己原来的衣服。
而且交换后美女手中的衣服数量必须和原来持有的衣服数量相同。

4 个回复

倒序浏览
楼主是需要算出解的个数么?
如果只是纯粹找解,那么只要吧5件衣服的集合和8件衣服的集合当做一个整体,
然后6件衣服的集合与7件衣服的集合当做另一个整体。
然后完全交换这两个集合里的衣服就可以了。
至于要算解的个数,设计到排列组合,楼主可以自己算算看。
回复 使用道具 举报
云惟桉 发表于 2012-5-18 01:15
楼主是需要算出解的个数么?
如果只是纯粹找解,那么只要吧5件衣服的集合和8件衣服的集合当做一个整体,
然 ...

嗯  谢谢啊   我写个看看
回复 使用道具 举报
本帖最后由 李哲 于 2012-5-18 19:48 编辑

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.Iterator;

  4. public class Cloth {

  5.         /**
  6.          * @param args
  7.          */
  8.         public static void main(String[] args) {
  9.                 // TODO Auto-generated method stub
  10.                 HashSet hs=new HashSet();
  11.                 while(true){               
  12.                 hs.add(new OneCloth("A",1 ));
  13.                 hs.add(new OneCloth("A",2 ));
  14.                 hs.add(new OneCloth("A",3 ));
  15.                 hs.add(new OneCloth("A",4 ));
  16.                 hs.add(new OneCloth("A",5 ));
  17.                
  18.                 hs.add(new OneCloth("B",1 ));
  19.                 hs.add(new OneCloth("B",2 ));
  20.                 hs.add(new OneCloth("B",3 ));
  21.                 hs.add(new OneCloth("B",4 ));
  22.                 hs.add(new OneCloth("B",5 ));
  23.                 hs.add(new OneCloth("B",6 ));
  24.                
  25.                 hs.add(new OneCloth("C",1 ));
  26.                 hs.add(new OneCloth("C",2 ));
  27.                 hs.add(new OneCloth("C",3 ));
  28.                 hs.add(new OneCloth("C",4 ));
  29.                 hs.add(new OneCloth("C",5 ));
  30.                 hs.add(new OneCloth("C",6 ));
  31.                 hs.add(new OneCloth("C",7 ));
  32.                
  33.                 hs.add(new OneCloth("D",1 ));
  34.                 hs.add(new OneCloth("D",2 ));
  35.                 hs.add(new OneCloth("D",3 ));
  36.                 hs.add(new OneCloth("D",4 ));
  37.                 hs.add(new OneCloth("D",5 ));
  38.                 hs.add(new OneCloth("D",6 ));
  39.                 hs.add(new OneCloth("D",7 ));
  40.                 hs.add(new OneCloth("D",8 ));
  41.                         
  42.                         ArrayList A=new ArrayList();
  43.                         
  44.                         Iterator it=hs.iterator();
  45.                         int as=0;               
  46.                         while (it.hasNext()&&as<5)
  47.                         {
  48.                                 OneCloth oc=(OneCloth)it.next();
  49.                                 if(oc.name!="A"){
  50.                                         A.add(oc);
  51.                                         it.remove();
  52.                                         as++;
  53.                                 }
  54.                                 
  55.                         }
  56.                         if(A.size()!=5)
  57.                                 continue ;
  58.                         
  59. //                        System.out.println("ok");
  60.                         ////////////////////////////////////
  61.                         
  62.                         
  63.                         
  64.                         ArrayList B=new ArrayList();
  65.                
  66.                         int bs=0;               
  67.                         while (it.hasNext()&&bs<6)
  68.                         {
  69.                                 OneCloth oc=(OneCloth)it.next();
  70.                                 if(oc.name!="B"){
  71.                                         B.add(oc);
  72.                                         it.remove();
  73.                                         bs++;
  74.                                 }
  75.                                 
  76.                         }
  77.                         if(B.size()!=6)
  78.                                 continue ;
  79.                         
  80. //                        System.out.println("ok");
  81.                         ////////////////////////////////////
  82.                         
  83.                         ArrayList C=new ArrayList();
  84.                         
  85.                         int cs=0;               
  86.                         while (it.hasNext()&&cs<7)
  87.                         {
  88.                                 OneCloth oc=(OneCloth)it.next();
  89.                                 if(oc.name!="C"){
  90.                                         C.add(oc);
  91.                                         it.remove();
  92.                                         cs++;
  93.                                 }
  94.                                 
  95.                         }
  96.                         if(C.size()!=7)
  97.                                 continue ;
  98.                         
  99. //                        System.out.println("ok");
  100.                         ////////////////////////////////////
  101.                         
  102.                 ArrayList D=new ArrayList();
  103.                         
  104.                         int ds=0;               
  105.                         while (it.hasNext()&&cs<8)
  106.                         {
  107.                                 OneCloth oc=(OneCloth)it.next();
  108.                                 if(oc.name!="D"){
  109.                                         D.add(oc);
  110.                                         it.remove();
  111.                                         ds++;
  112.                                 }
  113.                                 
  114.                         }
  115.                         if(D.size()!=8)
  116.                                 continue ;
  117.                         
  118. //                        System.out.println("ok");
  119.                         ////////////////////////////////////
  120.                         for(int i=0;i<5;i++){
  121.                         System.out.println("A--"+A.get(i).toString());}
  122.                         for(int i=0;i<6;i++){
  123.                                 System.out.println("B--"+B.get(i).toString());}
  124.                         for(int i=0;i<7;i++){
  125.                                 System.out.println("C--"+C.get(i).toString());}
  126.                         for(int i=0;i<8;i++){
  127.                                 System.out.println("D--"+D.get(i).toString());}
  128.                         break;
  129.                         
  130.                 }               

  131.         }

  132. }
  133. class OneCloth{
  134.         
  135.         String name;
  136.         int age;        
  137.         
  138.         public String getName() {
  139.                 return name;
  140.         }
  141.         public void setName(String name) {
  142.                 this.name = name;
  143.         }
  144.         public int getAge() {
  145.                 return age;
  146.         }
  147.         public void setAge(int age) {
  148.                 this.age = age;
  149.         }
  150.         @Override
  151.         public String toString() {
  152.                 return "OneCloth [name=" + name + ", age=" + age + "]";
  153.         }        
  154.         public OneCloth(String name, int age) {
  155.                 super();
  156.                 this.name = name;
  157.                 this.age = age;
  158.         }
  159.         
  160. }

  161. 最大的问题是hashSet的随机性很难保证。如果依靠一次一次的运行程代码,来一次一次的得到新的分配方法(因为hashSet的随机性)。这样做很不好,但是一次性得到所有非配方案,难度较大。
  162. 后来我设想将已有的方案(也就是将已经产生过的hashset存入数组)存入数组,下次再产生hashSet,如果和数组中的某个相同,就跳过。但是这个循环的停止条件我没有想到,而且可能会和代码内部的一些操作有冲突。我尝试,但是没有成功。
  163. 我想,仔细做一下,应该可以做到。
  164. 上面的代码只能通过一次次的运行代码,来得到不同的方案(依赖HashSet的随机性)。

  165. 在思路,continue,iterater的使用上面都有很大问题。
  166. 我这算是真正的抛砖引玉。
复制代码
回复 使用道具 举报
李哲 发表于 2012-5-18 18:47

:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马