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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xiong910627 中级黑马   /  2014-9-4 10:40  /  6768 人查看  /  40 人回复  /   6 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 xiong910627 于 2014-9-4 21:16 编辑

一位老农带着猫、狗、鱼过河,河边有一条船,
* 每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,
* 狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,
* 则不会发生这种问题。编程解决猫狗鱼过河问题。
  1. import java.util.ArrayList;

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

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

复制代码





40 个回复

倒序浏览
学习一下,我的测试题就是这个、
回复 使用道具 举报
大神呀,借鉴一下。。
回复 使用道具 举报
学习学习
回复 使用道具 举报
这个给力
回复 使用道具 举报
我也来赞一个
回复 使用道具 举报
学习下 啊
回复 使用道具 举报
为什么采用get(0);呢,是否最后add进去的s都是放在索引为0的位置?
回复 使用道具 举报
顶一个,学到了再来研究
回复 使用道具 举报
这么牛啊
回复 使用道具 举报
跟我的基础测试一样
回复 使用道具 举报
学习了谢谢分享
回复 使用道具 举报
许愿じ☆VE杰 来自手机 中级黑马 2014-9-4 22:22:11
13#
跟我的测试一样
回复 使用道具 举报
我也是这个题
回复 使用道具 举报
进来看看该怎么写
回复 使用道具 举报
学习一下
回复 使用道具 举报
额,看看了,我没这个题,听说这个题很火
回复 使用道具 举报
测试题最好不要发答案
回复 使用道具 举报
可惜还是要先自己理解一下的
回复 使用道具 举报
没看懂都
回复 使用道具 举报
123下一页
您需要登录后才可以回帖 登录 | 加入黑马