黑马程序员技术交流社区
标题:
大家帮我看看我这个程序有没有简单的做法???
[打印本页]
作者:
汪伟楠
时间:
2014-1-5 13:49
标题:
大家帮我看看我这个程序有没有简单的做法???
本帖最后由 汪伟楠 于 2014-1-11 11:59 编辑
package com.itheima;
import java.util.ArrayList;
/**
* 第10题:一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
* 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。
* 编程解决猫狗鱼过河问题.
* @param args
*/
//思路:先带猫过去,然后回来把狗带过,把猫带回来,再把鱼带过去,再回来带猫.
public class Test10 {
public static void main(String[] args) {
new Test10().tohere();
}
//创建一个集合,代表起始点.
ArrayList<String> here = new ArrayList<String>();
//创建一个集合,代表终点.
ArrayList<String> there = new ArrayList<String>();
//添加元素.
public Test10() {
here.add("Dog");
here.add("cat");
here.add("fish");
}
//定义一个方法,用来判断这三个动物之间关系.
public boolean isSafe(ArrayList<String> al) {
if (al.contains("dog") && al.contains("cat")
|| al.contains("cat") && al.contains("fish")) {
return false;
}
return true;
}
//定义一个方法,将起点的元素送到终点.
public void tohere() {
String str = here.get(0);
here.remove(str);
if (isSafe(here)) {
System.out.println("1农夫带着" + str + "去往对岸,这边还剩下" + here + ",对岸有"
+ there);
toThere(str);
} else {
here.add(str);
tohere();
}
}
//定义一个方法,用来查看终点的元素.
public void toThere(String s) {
if (isSafe(there)) {
there.add(s);
if(here.isEmpty()){
System.out.println("4农夫,"+there+"都被你带过来了");
return;
}
if(isSafe(there)){
System.out.println("2农夫回到原点,对岸有" + there);
tohere();
}else{
String temp=there.get(0);
there.remove(temp);
System.out.println("3农夫带着"+temp+"回到原点,这边有" + here + ",对岸有" + there);
here.add(temp);
tohere();
}
} else {
there.remove(s);
tohere();}
}
}
作者:
jibenwujie
时间:
2014-1-5 14:13
这道题在我基础测试的时候也做过,
但是我只有9.5分
我贴上代码给你参考一下吧,
package com.itheima;
public class Test10 {
/**
* 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。
* 当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。 编程解决猫狗鱼过河问题
*/
public static void main(String[] args) {
// 猫狗鱼的数组
String[] animal = { "猫", "狗", "鱼" };
// 循环出所有过河的可能
for (int i = 0; i < animal.length; i++) {
for (int j = 0; j < animal.length; j++) {
for (int k = 0; k < animal.length; k++) {
// 排除掉 猫猫猫 狗狗狗等组合...
if (i != j && i != k && k != j) {
System.out.print("组合方式:" + animal[i] + "," + animal[j]
+ "," + animal[k]);
// 因为食物级别,作为中间的猫必须先带走,排除掉先带走狗和鱼的可能
if (animal[i] == "狗") {
System.out.println("\t猫吃掉了鱼");
} else if (animal[i] == "鱼") {
System.out.println("\t狗吃掉了猫");
} else {
System.out.println("\t渡河成功");
}
}
}
}
}
}
}
复制代码
作者:
ixiangfeng
时间:
2014-1-5 14:16
这是基础测试还是入学考试的题?
作者:
午夜b'Boy
时间:
2014-1-5 16:21
这题我也做了,代码太复杂了,你们的简单多了,受教了
作者:
午夜b'Boy
时间:
2014-1-5 21:05
今天写了半天
package com.itheima;
import java.util.ArrayList;
public class River3 {
//此变量用来标示正确的步数,
public static int count=1;
/**
* @param args
*
* 思路:
*
* 1,狗用“狗猫”代替,猫用”猫鱼“代替,鱼用“鱼儿”代替,首字符代表自己,
* 尾字符代表其食物;方便互斥判断
* 2,new两个ArrayList<string>:Left和Right,分别代表河左岸,右岸;
*,3,Left中存储三个动物,都移到Right集合则完成任务;
* 4,
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr={"狗","猫","鱼"};
//字符替换动作,方便做互斥判断,
for(int i=0;i<arr.length;i++){
if(arr[i].equals("狗"))
arr[i]="狗猫";
else{
if(arr[i].equals("猫"))
arr[i]="猫鱼";
else{
if(arr[i].equals("鱼"))
arr[i]="鱼儿";
}
}
}
ArrayList<String> lift=new ArrayList<String>();
ArrayList<String> right=new ArrayList<String>();
//各动物到达左岸;
for(String s : arr){
lift.add(s);
}
System.out.println("各动物到达左岸,准备渡河:");
show(lift,right);
System.out.println();
//开始渡河,若右岸集合有三个元素,任务完成程序结束
while(right.size()!=3){
String animal=lift.remove(0);
//左岸移出,右岸添入;
right.add(animal);
//调用test()方法,测试两集合的互斥情况,返回布尔值进行处理;
//发生互斥,右岸移回一个动物,否则不做任何动作,程序继续;
if(!test(lift)){
animal=right.remove(0);
lift.add(animal);
}else{
System.out.println(animal.charAt(0)+"送到右岸!");
show(lift,right);
}
if(!test(right)){
System.out.println(right.get(0).charAt(0)+"送回左岸!");
animal=right.remove(0);
lift.add(animal);
show(lift,right);
}
}
System.out.println("所有动物来到了右岸!!");
}
//两集合进行互斥处理;
public static boolean test(ArrayList<String> list){
boolean b=true;
if(list.size()==3)
return b;
String s1="";
String s2="";
if(list.size()==2){
s1=list.get(0);
s2=list.get(1);
if(s1.contains(s2.charAt(1)+"")){
b=false;
}
if(s2.contains(s1.charAt(1)+"")){
b=false;
}
}
return b;
}
public static void show(ArrayList<String> lift,ArrayList<String> right){
StringBuilder sb=new StringBuilder("左岸为:");
if(lift.size()>0){
for(String s : lift){
sb.append(s.charAt(0));
}
}else{
sb.append("空");
}
sb.append(";右岸为:");
if(right.size()>0){
for(String s : right){
sb.append(s.charAt(0));
}
}else{
sb.append("空");
}
System.out.println(sb.toString());
System.out.println();
}
}
复制代码
才写出来这题;
作者:
午夜b'Boy
时间:
2014-1-5 21:17
package com.itheima;
import java.util.ArrayList;
public class River3 {
//此变量用来标示正确的步数,
public static int count=1;
/**
* @param args
*
* 思路:
*
* 1,狗用“狗猫”代替,猫用”猫鱼“代替,鱼用“鱼儿”代替,首字符代表自己,
* 尾字符代表其食物;方便互斥判断
* 2,new两个ArrayList<string>:Left和Right,分别代表河左岸,右岸;
*,3,Left中存储三个动物,都移到Right集合则完成任务;
* 4,
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] arr={"狗","猫","鱼"};
//字符替换动作,方便做互斥判断,
for(int i=0;i<arr.length;i++){
if(arr[i].equals("狗"))
arr[i]="狗猫";
else{
if(arr[i].equals("猫"))
arr[i]="猫鱼";
else{
if(arr[i].equals("鱼"))
arr[i]="鱼儿";
}
}
}
ArrayList<String> lift=new ArrayList<String>();
ArrayList<String> right=new ArrayList<String>();
//各动物到达左岸;
for(String s : arr){
lift.add(s);
}
System.out.println("各动物到达左岸,准备渡河:");
show(lift,right);
System.out.println();
//开始渡河,若右岸集合有三个元素,任务完成程序结束
while(right.size()!=3){
String animal=lift.remove(0);
//左岸移出,右岸添入;
right.add(animal);
//调用test()方法,测试两集合的互斥情况,返回布尔值进行处理;
//发生互斥,右岸移回一个动物,否则不做任何动作,程序继续;
if(!test(lift)){
animal=right.remove(0);
lift.add(animal);
}else{
System.out.println(animal.charAt(0)+"送到右岸!"+"第"+(count++)+"步!");
show(lift,right);
}
if(!test(right)){
System.out.println(right.get(0).charAt(0)+"送回左岸!"+"第"+(count++)+"步!");
animal=right.remove(0);
lift.add(animal);
show(lift,right);
}
}
System.out.println("所有动物来到了右岸!!");
}
//两集合进行互斥处理;
public static boolean test(ArrayList<String> list){
boolean b=true;
if(list.size()==3)
return b;
String s1="";
String s2="";
if(list.size()==2){
s1=list.get(0);
s2=list.get(1);
if(s1.contains(s2.charAt(1)+"")){
b=false;
}
if(s2.contains(s1.charAt(1)+"")){
b=false;
}
}
return b;
}
public static void show(ArrayList<String> lift,ArrayList<String> right){
StringBuilder sb=new StringBuilder("左岸为:");
if(lift.size()>0){
for(String s : lift){
sb.append(s.charAt(0));
}
}else{
sb.append("空");
}
sb.append(";右岸为:");
if(right.size()>0){
for(String s : right){
sb.append(s.charAt(0));
}
}else{
sb.append("空");
}
System.out.println(sb.toString());
System.out.println();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2