黑马程序员技术交流社区
标题:
无语,这怎么搞
[打印本页]
作者:
张东贤
时间:
2013-3-21 01:26
标题:
无语,这怎么搞
本帖最后由 张东贤 于 2013-3-21 12:56 编辑
老伯伯要带鱼、狗、猫过河到
对岸.,有一条船,只能坐一个人,
老伯每次只能带一样动物过河,当
老伯不在的时侯狗会咬猫,猫会吃
鱼.,请问怎么顺序过河呢?要求:编
写程序,由程序来推出过河的顺序
这怎么用对象来搞啊??
谢谢,有想法了,稍后贴代码
作者:
王洪宇
时间:
2013-3-21 08:36
来个不用程序的:
1.先把猫带过去,回来;
2.再把狗带过去,把猫带回来;
3.把鱼带过去,回来;
4.把猫带过去。
作者:
android2050
时间:
2013-3-21 08:52
import java.util.Iterator;
import java.util.LinkedList;
public class AcrossTheRiver {
// 定义三个String对象
public static final String rabbitName = "Rabbit";
public static final String wolfName = "Wolf";
public static final String cabbageName = "Cabbage";
// 判断两个货物之间关系是否友好 写的麻烦了一点= =..
public static boolean isFriendly(Goods goods1, Goods goods2) {
if (goods1 != null) {
if (goods1.getGoodsName().trim().equals(rabbitName)) {
if (goods2 == null) {
return true;
} else {
return false;
}
} else if (goods1.getGoodsName().trim().equals(wolfName)) {
if (goods2 == null || goods2.getGoodsName().trim().equals(cabbageName)) {
return true;
} else {
return false;
}
} else if (goods1.getGoodsName().trim().equals(cabbageName)) {
if (goods2 == null || goods2.getGoodsName().trim().equals(wolfName)) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return true;
}
}
// 我就直接写在主方法里了
public static void main(String[] args) {
boolean isSuccess = false;
LinkedList<Goods> beforeCrossing = new LinkedList<Goods>();
LinkedList<Goods> afterCrossing = new LinkedList<Goods>();
beforeCrossing.add(new Goods(rabbitName));
beforeCrossing.add(new Goods(cabbageName));
beforeCrossing.add(new Goods(wolfName));
while (!isSuccess) {
Goods goods1 = beforeCrossing.getFirst();
System.out.println(goods1.getGoodsName() + " 被取走了");
beforeCrossing.removeFirst();
if (beforeCrossing.isEmpty()) {
afterCrossing.addLast(goods1);
isSuccess = true;
System.out.println("全部移动完毕!");
} else {
Iterator<Goods> it = beforeCrossing.iterator();
Goods[] beforeCro = new Goods[2];
for (int i = 0; it.hasNext(); i++) {
beforeCro[i] = it.next();
System.out.println(beforeCro[i].getGoodsName() + " 留了下来");
}
if (isFriendly(beforeCro[0], beforeCro[1])) {
if (afterCrossing.isEmpty()) {
afterCrossing.addLast(goods1);
System.out.println(goods1.getGoodsName() + " 被成功的放到了对岸");
} else {
Goods goods2 = afterCrossing.getFirst();
if (isFriendly(goods1, goods2)) {
afterCrossing.addLast(goods1);
System.out.println(goods1.getGoodsName() + " 被成功的放到了对岸");
} else {
beforeCrossing.addLast(goods2);
afterCrossing.removeFirst();
System.out.println(goods1.getGoodsName() + " 与 "
+ goods2.getGoodsName() + "并不和睦 于是把 " + goods2.getGoodsName()
+ "带了回来 并将 " + goods1.getGoodsName() + " 留了下来");
}
}
} else {
beforeCrossing.addLast(goods1);
System.out.println("很可惜 留下来的两个东西并不和睦 于是 " + goods1.getGoodsName()
+ " 又被放了回去");
}
}
}
}
}
// 货物类
class Goods {
// 货物名称
private String goodsName;
// 默认构造方法
public Goods(String goodsName) {
this.goodsName = goodsName;
}
// 获得货物名称
public String getGoodsName() {
return goodsName;
}
}
复制代码
作者:
梁耀今
时间:
2013-3-21 08:55
陈团辉 发表于 2013-3-21 08:52
这个跟百度的一模一样,就是改了一下名字!靠!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2