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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wodenhaowzg 中级黑马   /  2014-1-17 18:04  /  1131 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 wodenhaowzg 于 2014-1-17 18:40 编辑

一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题。有木有大神能把这道题写的简单点。。网上复杂的代码我就不贴了。。自己做也是很费劲

评分

参与人数 1技术分 +1 收起 理由
黄晓鑫 + 1

查看全部评分

2 个回复

倒序浏览
  1. //思路:先带猫过去,然后回来把狗带过,把猫带回来,再把鱼带过去,再回来带猫.       
  2. public class Test10 {
  3.        
  4.         public static void main(String[] args) {
  5.                 new Test10().tohere();
  6.         }
  7.                 //创建一个集合,代表起始点.
  8.                 ArrayList<String> here = new ArrayList<String>();
  9.                 //创建一个集合,代表终点.
  10.                 ArrayList<String> there = new ArrayList<String>();
  11.                 //添加元素.
  12.                 public Test10() {
  13.                         here.add("Dog");
  14.                         here.add("cat");
  15.                         here.add("fish");
  16.                 }
  17.                 //定义一个方法,用来判断这三个动物之间关系.
  18.                 public boolean isSafe(ArrayList<String> al) {
  19.                         if (al.contains("dog") && al.contains("cat")
  20.                                         || al.contains("cat") && al.contains("fish")) {
  21.                                 return false;
  22.                         }
  23.                         return true;
  24.                 }

  25.                 //定义一个方法,将起点的元素送到终点.
  26.                 public void tohere() {
  27.                         String str = here.get(0);
  28.                         here.remove(str);
  29.                         if (isSafe(here)) {
  30.                                 System.out.println("1农夫带着" + str + "去往对岸,这边还剩下" + here + ",对岸有"
  31.                                                 + there);
  32.                                 toThere(str);
  33.                         } else {
  34.                                 here.add(str);
  35.                                 tohere();
  36.                         }
  37.                 }
  38.                 //定义一个方法,用来查看终点的元素.
  39.                 public void toThere(String s) {
  40.                        
  41.                         if (isSafe(there)) {
  42.                                 there.add(s);
  43.                                 if(here.isEmpty()){
  44.                                         System.out.println("4农夫,"+there+"都被你带过来了");
  45.                                         return;
  46.                                 }
  47.                                 if(isSafe(there)){
  48.                                         System.out.println("2农夫回到原点,对岸有" + there);
  49.                                         tohere();
  50.                                 }else{
  51.                                         String temp=there.get(0);
  52.                                         there.remove(temp);
  53.                                         System.out.println("3农夫带着"+temp+"回到原点,这边有" + here + ",对岸有" + there);
  54.                                         here.add(temp);
  55.                                         tohere();
  56.                                 }
  57.                         } else {
  58.                                 there.remove(s);
  59.                                 tohere();}
  60.                         }
  61.         }
复制代码

这是基础测试题吧!我也做了这道题!你借鉴借鉴吧!

评分

参与人数 1技术分 +1 收起 理由
黄晓鑫 + 1

查看全部评分

回复 使用道具 举报 1 0
嗯 代码试了 不错 这代码是你写的么 厉害
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马