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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ‍杨博 中级黑马   /  2014-6-9 21:10  /  1129 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 ‍杨博 于 2014-6-13 10:01 编辑

java基础题中有一道老农过河的题目,看到后没有思路。就是无法把逻辑思维准确的转化成程序语言。如果这方面很弱,需要加强哪方面的训练呢。请不吝赐教,谢谢。
谢谢各位的解答,提问结束。

3 个回复

正序浏览
当初基础测试就跪在这一题,后来是看了别人的思路才把它写出来,这题是先把思路理清才好写
回复 使用道具 举报
刚开始我也是这样,学的时候很清晰,自己看题要敲代码了就没思路了,也无从下手!!!
回复 使用道具 举报
  1. //一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,则不会发生这种问题。编程解决猫狗鱼过河问题。
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. //先带猫过去,然后回来把狗带过,把猫带回来  
  5. //再把鱼带过去,再回来带猫  
  6. public class Test10
  7. {  
  8.     ArrayList<String> here = new ArrayList<String>();  
  9.     ArrayList<String> there = new ArrayList<String>();  
  10.     public boolean isSafty(List list)
  11.     {  
  12.         if (list.contains("dog") && list.contains("cat")  
  13.                || list.contains("cat") && list.contains("fish"))
  14.                    {  
  15.            return false;         
  16.            }  
  17.         return true;  
  18.    }  
  19.   
  20.     public Test10()
  21.     {  
  22.         here.add("Dog");  
  23.         here.add("cat");  
  24.        here.add("fish");  
  25.     }  
  26.     public void toTake()
  27.     {  
  28.        String str = here.get(0);  
  29.       here.remove(str);  
  30.       if (isSafty(here))
  31.       {  
  32.            System.out.println("1农夫带着" + str + "去往对岸,这边还剩下" + here + ",对岸有"  + there);  
  33.                            toThere(str);      
  34.       }
  35.       else
  36.       {  
  37.            here.add(str);  
  38.           toTake();  
  39.       }  
  40.     }   
  41.     public void toThere(String s)
  42.     {  
  43.             if (isSafty(there))
  44.             {  
  45.                     there.add(s);  
  46.             if(here.isEmpty())
  47.             {  
  48.                System.out.println("农夫,"+there+"都被你带过来了");  
  49.                 return;  
  50.             }  
  51.            if(isSafty(there))
  52.            {  
  53.                 System.out.println("2农夫回到原点,对岸有" + there);  
  54.                toTake();  
  55.            }
  56.                                 else
  57.                                 {  
  58.                 String temp=there.get(0);  
  59.                there.remove(temp);  
  60.                System.out.println("3农夫带着"+temp+"回到原点,这边有" + here + ",对岸有" + there);  
  61.                 here.add(temp);  
  62.                 toTake();  
  63.                                 }  
  64.         }
  65.                                 else
  66.                                 {  
  67.                                 there.remove(s);  
  68.                                 toTake();  
  69.                         }  
  70.     }  
  71.     public static void main(String[] args)
  72.     {  
  73.        new Test10().toTake();  
  74.     }  
  75. }  
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马