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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lwy0319   /  2014-3-10 12:40  /  9587 人查看  /  22 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

牛呀!!!!!
回复 使用道具 举报
cuijinghao 来自手机 中级黑马 2015-9-18 13:03:14
22#
依然不知道过河是什么意思
回复 使用道具 举报
不用那么长代码的,亲们。
这晚我刚刚写的,讨论学习。

  1. import java.util.LinkedList;

  2. public class Ferry {

  3.         public static void main(String[] args) {
  4.                 LinkedList<String> here = new LinkedList<String>();
  5.                 LinkedList<String> there = new LinkedList<String>();
  6.                 String cat = "猫";
  7.                 String dog = "狗";
  8.                 String fish = "鱼";
  9.                 int count = 0;
  10.                 here.add(cat);
  11.                 here.add(dog);
  12.                 here.add(fish);
  13.                 while(there.size()<3){
  14.                         System.out.println("----第"+(++count)+"次摆渡开始----");
  15.                         String boat = null;
  16.                         while(true){
  17.                                 boat = here.removeFirst();
  18.                                 if(isNice(here)){
  19.                                         there.addLast(boat);
  20.                                         System.out.println("老农正把("+boat+")送往河对面....");
  21.                                         break;
  22.                                 }
  23.                                 here.addLast(boat);
  24.                         }
  25.                         if(!isNice(there)&&there.size()<3){
  26.                                         boat = there.removeFirst();
  27.                                         System.out.println("并把("+boat+")往回送....");
  28.                                         here.addLast(boat);
  29.                                 }
  30.                         System.out.println("河这面:"+here);
  31.                         System.out.println("河对面:"+there);
  32.                 }
  33.         }
  34.         public static boolean isNice(LinkedList<String> li){
  35.                 if(li.contains("猫")&&li.contains("鱼"))
  36.                         return false;
  37.                 else if(li.contains("狗")&&li.contains("猫"))
  38.                         return false;
  39.                 else
  40.                         return true;
  41.         }
  42. }
复制代码
回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马