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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 田磊阳   /  2013-4-9 23:41  /  7185 人查看  /  90 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*

  2. */
  3. package cn.yy.practice;

  4. import java.util.*;

  5. /**
  6. * 既然母牛生小牛,就用Cow类代表牛,提及时间,用年龄age表示,刚出生age=0.
  7. * 还应该提供一个bear()方法,返回一个新的Cow对象,表示生了一头牛.
  8. * 使用List集合来存储所有的牛,每遍历一次,表示过了一年,增加一个变量currentYear表示当前年份
  9. * 遍历过程中,判断每个Cow的age值,如果大于或等于3,生育一头小牛,并将这头小牛也存入集合
  10. * 遍历后增加一个变量currentCount记录当前集合中总共有多少Cow对象
  11. * 最后返回currentCount,表示指定年份n后总共有多少牛.
  12. */
  13. public class CowProblem {
  14.        
  15.         List<Cow> cows = new ArrayList<Cow>();           //每生一头牛,存入集合

  16.         public int count(int year){                                        //指定年份

  17.                 int currentYear = 0;                                        //当前年份

  18.                 cows.add(new Cow());                                        //加入第一头母牛
  19.                
  20.                 int currentCount = cows.size();                        //当前集合中元素个数.
  21.                                                                                                

  22.                 while((currentYear++) < year ){                       

  23.                         for (int i=0; i<currentCount; i++ )
  24.                         {
  25.                                 Cow c = cows.get(i);                        //取出一个Cow对象,并将年龄加1
  26.                                
  27.                                 if(++c.age >= 3)                                //如果大于或等于3,则产生一个新Cow对象,并存入集合
  28.                                         cows.add(c.bear());                               
  29.                         }
  30.                         currentCount = cows.size();                        //每次遍历后记录下当前集合内的元素个数,
  31.                                                                                                 //防止下一次遍历过程中,遍历到了新产生的牛
  32.                 }
  33.        
  34.                 return currentCount;
  35.                
  36.         }               

  37.         public static void main(String[] args) {
  38.                 Scanner input = new Scanner(System.in);

  39.                 System.out.print("input an integer(year) : ");

  40.                 int year = 0;
  41.                 int sum = 0;
  42.                 try{
  43.                         year = input.nextInt();                       //指定年份               

  44.                         if(year >= 0)
  45.                                 sum = new CowProblem().count(year);

  46.                         System.out.println("cow count = " + sum);                //打印结果

  47.                 }catch(InputMismatchException e){
  48.                         System.out.println("not an integer, or is out of range");
  49.                 }                                                       
  50.                
  51.                
  52.         }
  53.        
  54. }

  55. class Cow {                                                        //定义一个Cow类,
  56.                
  57.                 int age;

  58.                 public Cow(){                                //初始化age
  59.                         age = 0;
  60.                 }       
  61.                 public Cow bear(){                        //bear()方法,返回一个新的Cow对象,表示生了一头牛.
  62.                         return new Cow();
  63.                 }
  64.         }
复制代码

CowProblem.zip

1.16 KB, 下载次数: 34

评分

参与人数 1技术分 +4 收起 理由
田磊阳 + 4

查看全部评分

回复 使用道具 举报
  1. package choux;

  2. import java.util.ArrayList;
  3. import java.util.List;

  4. public class Cow {

  5. private int age = 0;

  6. /**
  7. * 是否可以生产小牛
  8. */
  9. public boolean isCreatSmallCow(){
  10.    return (age >= 3)?true:false;
  11. }

  12. /**
  13. * 过年了,年龄又大了一岁
  14. */
  15. public void addYear(){
  16.    age++;
  17. }
  18. public static void main(String[] args) {
  19.    List<Cow> cowList = new ArrayList<Cow>();
  20.    //第一头小牛
  21.    cowList.add(new Cow());
  22.    int yearCount = 10;
  23.    //就这样一年年过
  24.    for(int i=1;i<=yearCount;i++){
  25.     int rowNum = cowList.size();
  26.     for(int j = 0; j<rowNum; j++){
  27.      Cow o = cowList.get(j);
  28.      //过年了
  29.      o.addYear();
  30.      //能生小牛吗?
  31.      if(o.isCreatSmallCow()){
  32.       cowList.add(new Cow());
  33.      }
  34.     }
  35.    }
  36.    System.out.println(yearCount+"年后将有【"+cowList.size()+"】头牛。");
  37. }
  38. }

复制代码

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
本帖最后由 高新星 于 2013-4-11 14:51 编辑
  1. /*
  2. * 需求:一个农夫养了一头牛,三年后,这头牛每一年会生出1头牛,生出来的牛三年后,
  3. *                         又可以每年生出一头牛……问农夫10年后有多少头牛?n年呢?(不考虑其他因素,只考虑此数学问题)
  4. *
  5. * 思路:
  6. * 1,定义一个计算牛的数量的功能函数getCattleNum();
  7. * 2,确定函数的结果为牛的数量,所以该函数的返回结果为 int
  8. * 3,确定函数的需要调用者传入的参数为几年,所以该函数的参数类型和个数确定为 int ,且个数为一
  9. * 4,定义变量sum表示牛的数量
  10. * 5,牛的总数等于前一年getCattleNum(year-1)的数量加上新出生的牛的数量
  11. * 6,新出生牛的数量根据题意可以确定为三年前牛的数量gwtCattleNum(year-3),因为每头牛都是在三年后才会产牛
  12. * 7,根据5,6可以确定该函数用的是递归的方法
  13. */

  14. public class TeatCattle {
  15.         //定义变量牛的总数sum
  16.         static int sum;

  17.         /*
  18.          * 定义获取牛的数量的函数
  19.          */
  20.         public static int getCattleNum(int year){
  21.                
  22.                 for(int x = 1;x <=year;x++){
  23.                         //判断年数是否大于三,小于三直接返回牛的数量为1
  24.                         if(year <= 3)
  25.                                 sum=1;                                                                                        //初始化时的牛的数量
  26.                         else
  27.                                 sum = getCattleNum(year-3) +getCattleNum(year-1);
  28.                 }
  29.                
  30.                 return sum;                                                                                        //返回牛的总数
  31.         }
  32.         
  33.         
  34.         
  35.         public static void main(String[] args) {
  36.                 //测试十年后的牛的数量
  37.                 int year = 10;
  38.                 int sum = getCattleNum(year);
  39.                 System.out.println(sum);//数量为:19

  40.         }
  41. }
复制代码

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
  1. public class Test01
  2. {
  3.         static int sum = 0;
  4.         public static void main(String[] args)
  5.         {
  6.                 method(10);
  7.                 System.out.println("总数:"+sum);
  8.         }

  9.         public static void method(int n){
  10.                 if(n==0)
  11.                         return;
  12.                 sum+=n;
  13.                 method(--n);               
  14.         }
  15. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
田磊阳 + 1

查看全部评分

回复 使用道具 举报
public class Cow
{
public static int count = 0;
public Cow(int year)
{
count++;
for(int i=3+year;i<=10;i++)
{
new Cow(i);
}
}

public static void main(String[] args)
{
new Cow(0);
System.out.println(count);
}
}
面向对象方法!

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
表示基础视频只学了一半,
我尽力了。

Test.rar

1.85 KB, 阅读权限: 100, 下载次数: 2

答案

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
已做完,不知这样格式对不对,请过目。

黄杨.zip

904 Bytes, 阅读权限: 100, 下载次数: 1

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
本帖最后由 王永贺 于 2013-4-11 21:18 编辑

此题和兔子的那道题很类似都是用递归的方法解决   这道题我刚发贴的时候我就回复过,我当时回复到发帖的里面了 随后应该被删除了吧  所以我现在重新回复下
  1. public class cow {
  2.         static int cow_count = 1;
  3.         private static void fn(int year,int age,int n){        //year代表第几年, age代表牛的年龄,能代表最大年限
  4.                 year++;
  5.                 age++;
  6.                 if(year<=n){  //如果没有达到上限年限则执行下面语句
  7.                         if(age>=3){ //小牛的年龄大于三岁,每年生一头牛
  8.                                 cow_count++;  //牛的总数加1
  9.                                 fn(year,0,n);  //递归刚出生的小牛年龄设置为0
  10.                         }
  11.                         fn(year,age,n);
  12.                 }
  13.         }

  14.         public static void main(String[] args) {
  15.                 new cow().fn(0, 0,10);
  16.                 System.out.println(cow_count);
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
/*
一个农夫养了一头牛,三年后,这头牛每一年会生出1头牛,生出来的牛三年后,
又可以每年生出一头牛……问农夫10年后有多少头牛?n年呢?(不考虑其他因素,只考虑此数学问题)



year:1        2        3        4        5        6        7        8        9        10
                        1        1        1        1        1        1        1               
                                                1        1        1        1
                                                                        1
                                                                               
思路:第一头牛三年后,从第四年开始生牛,十年时间一个生了7头;
        第二头牛开始从第七年开始生牛,生了4头
       
                 ......(以此类推)

        所以,此方法得用递归!
*/

class  Demo2
{
        public static void main(String[] args)
        {
                System.out.println(method(10)+1);        //method(10)+1;   加1是指最开始那头牛;
        }
       
        public static int method(int year)       
        {
                int num=0;                //num,用于存储牛的个数
               
                year=year-3;        //一头新牛过三年才开始生牛,每年一头;
               
                num=num+year;        //牛的个数加上三年后牛每一年生的牛个数;

                if((year-3)>0)        //这里是递归结束的条件。
                        num=num+method(year);        //重新调用method()
               
                return num;                //返回值,牛的个数
        }
}

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
  1. import java.util.ArrayList;
  2. import java.util.List;

  3. /**
  4. * n年的问题,只要将程序中的year改成n年即可
  5. *
  6. * @author LeeYou
  7. *
  8. */
  9. public class Cow {
  10.         private int age = 1; // 初始年龄为1岁
  11.         private static List<Cow> cowList = new ArrayList<Cow>();// 定义一个 集合容器,用于装牛

  12.         /**
  13.          * 判断是否可以繁殖小牛
  14.          *
  15.          * @return
  16.          */
  17.         public boolean canBreedCow() {
  18.                 return (age > 3) ? true : false;
  19.         }

  20.         /**
  21.          * 岁数叫1
  22.          */
  23.         public void addYear() {
  24.                 age++;
  25.         }

  26.         /**
  27.          * 产牛工具函数
  28.          *
  29.          * @param year
  30.          */
  31.         public static void BreedCow() {
  32.                 cowList.add(new Cow());
  33.         }

  34.         public static void main(String[] args) {

  35.                 cowList.add(new Cow());// 初始为1头牛
  36.                 int year = 10;// 年数10年
  37.                 // 外层循环控制年数
  38.                 for (int i = 1; i <= year; i++) {
  39.                         int cowCount = cowList.size();
  40.                         // 内层循环获取每头牛的对象,并判断是否可以繁殖小牛
  41.                         for (int j = 0; j < cowCount; j++) {
  42.                                 Cow cow = cowList.get(j);// 逐个获取每头牛的对象
  43.                                 cow.addYear();// 长大一岁
  44.                                 // 如果年龄大于三岁,则每年可以繁殖一头小牛
  45.                                 if (cow.canBreedCow()) {
  46.                                         BreedCow();
  47.                                 }
  48.                         }
  49.                 }

  50.                 // 输出容器的容量即为牛的总头数
  51.                 System.out.println(year + "年后总共有【" + cowList.size() + "】头牛");
  52.         }

  53. }
复制代码

评分

参与人数 1技术分 +4 收起 理由
田磊阳 + 4

查看全部评分

回复 使用道具 举报
import java.util.ArrayList;
import java.util.List;


public class Cow {

private int age = 0;

/**
* 是否可以生产小牛
* @return True | False
*/
public boolean isCreatSmallCow(){
   return (age >= 3)?true:false;
}

/**
* 过年了,年龄又大了一岁
*/
public void addYear(){
   age++;
}

/**
* @param args
*/
public static void main(String[] args) {
   List<Cow> cowList = new ArrayList<Cow>();
   //第一头小牛
   cowList.add(new Cow());
   int yearCount = 10;
   //就这样一年年过
   for(int i=1;i<=yearCount;i++){
    int rowNum = cowList.size();
    for(int j = 0; j<rowNum; j++){
     Cow o = cowList.get(j);
     //过年了
     o.addYear();
     //能生小牛吗?
     if(o.isCreatSmallCow()){
      cowList.add(new Cow());
     }
    }
   }
   System.out.println(yearCount+"年后将有【"+cowList.size()+"】头牛。");
}
}

------十年有28头

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
本帖最后由 艾萱 于 2013-4-12 00:18 编辑
  1. public class CountBulls {
  2.         public static void main(String[] args){
  3.                 System.out.println(coutChildren(10) + 1);//孩子有了,加上孩子妈
  4.         }
  5.         /**
  6.          * 计算经过 指定年数后 的孩子总数
  7.          * @param num 指定年数
  8.          */
  9.         public static int coutChildren(int num){
  10.                 int count = 0;
  11.                 //每过三年才会有新的小牛 第一次生小牛,在计算小牛的孩子后,看看它的孩子有没有生小牛
  12.                 //函数调用自己无限深入下一代去检查,直到下一代还没有到生小牛的年纪
  13.                 if(num >= 3)
  14.                         count = (num + 1 - 3) + coutChildren(num - 3);
  15.                 return count;
  16.         }
  17. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
  1. /*
  2. 题目描述:
  3. 一个农夫养了一头牛,三年后,这头牛每一年会生出1头牛,生出来的牛三年后,
  4. 又可以每年生出一头牛……问农夫10年后有多少头牛?n年呢?(不考虑其他因素,只考虑此数学问题)
  5. */
  6. /*
  7. 思路.
  8. 1.定义一个变量来存贮一共牛的数量 num
  9. 2.因为牛生牛,每次间隔都会有三年.所以在定义一个变量num1
  10. 3.遍历牛.年份减去3就是第一头牛所生的所以小牛.然后小牛生小牛,又是三年后的事.所以用第一批牛的总数减3来当第三批牛的"年"
  11. */

  12. class Demo
  13. {
  14.         public static void main(String[] args)
  15.         {
  16.                 //定义一个cattle函数.往里面扔"年",返回多少牛
  17.                 int n=cattle(9);
  18.                 System.out.println(n);//打印牛
  19.         }
  20.         public static int cattle(int i)
  21.         {       
  22.                 /*
  23.                 定义两个变量,一个为牛的总数.另一个为下批牛的变量
  24.                 */
  25.                 int num1=0;
  26.                 int num=1;
  27.                 for(int x=0;x<=i-3;x++)
  28.                 {
  29.                         num++;
  30.                         if(num==i-3)//当牛的总数加到年减三的时候.就赋值给下批牛的变量.
  31.                         {
  32.                                 num1=num;
  33.                                 num=num+cattle(num1);//使用递归
  34.                         }
  35.                 }               
  36.                 return num;
  37.         }
  38. }
  39. /*
  40. ps:版主出的题目也太绕人了吧....我足足研究了两个小时....主要是考数学啊!!!!
  41. 应该是对的了.....如果哪里不对还请帮忙给我通通哈...谢谢啦.
  42. */
复制代码

未命名.jpg (67.94 KB, 下载次数: 6)

未命名.jpg

评分

参与人数 1技术分 +4 收起 理由
田磊阳 + 4

查看全部评分

回复 使用道具 举报
Sword 金牌黑马 2013-4-12 16:21:57
74#
  1. 1递归(结果为28,如果要算n年后的结果,只需将10改为n):

  2. public class Cow {
  3.         static int count = 1;
  4.         private static void feedCow(int year,int age){
  5.                 year++;
  6.                 age++;
  7.                 if(year<=30){
  8.                         if(age>=3){
  9.                                 count++;
  10.                                 feedCow(year,0);
  11.                         }
  12.                         feedCow(year,age);
  13.                 }
  14.         }

  15.         public static void main(String[] args) {
  16.                 new Cow().feedCow(0, 0);
  17.                 System.out.println(count);
  18.         }
  19. }
复制代码
2.面向对象(很经典的思路,结果当然是28,要求n年后,只需将i<10改为i< p>
  1. public class Cow {
  2.         public static int count = 0;
  3.         public Cow(int year){
  4.                 count++;
  5.                 for(int i=3+year;i<=10;i++){
  6.                         new Cow(i);
  7.                 }
  8.         }

  9.         public static void main(String[] args) {
  10.                 new Cow(0);
  11.                 System.out.println(count);
  12.         }
  13. }
复制代码
30年后居然可以生出58425头牛…

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
  1. public class Test
  2. {       
  3.         private static  int COUNT=1;

  4.         public static void main(String a[]){
  5.         calve(10);
  6.         outSum();
  7.         }

  8.         public static void calve(int n){
  9.                 for(int i=1;i<=n;i++){
  10.                         if(i>3){
  11.                                 COUNT++;
  12.                                 calve(n-i);
  13.                         }
  14.                 }
  15.         }

  16.         public static void outSum(){
  17.                 System.out.println("牛的头数为:"+COUNT);
  18.         }

  19. }
复制代码
因为版主说3年后,我就从第四年开始算的。没有包括第三年。楼主辛苦了

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
发现不对,换了一种思路,再来一次
  1. public class CountBulls {
  2.         public static void main(String[] args){               
  3.                 new CountBulls(0);//调用构造函数传入一个0
  4.                 System.out.println("10年后一共有: " + count + "头牛");//输出最后牛的总数
  5.         }
  6.                
  7.         public static int count = 0;//定义一头牛初始为0,static修饰为了主函数调用方便
  8.         public CountBulls(int year){
  9.                 count++;//累加牛的数量
  10.                 //每过三年才会有新的小牛 第一次生小牛,一共10年
  11.                 for(int i=3+year;i<=10;i++){
  12.                         new CountBulls(i);//函数调用自己无限深入下一代去检查,直到下一代还没有到生小牛的年纪
  13.                 }
  14.         }
  15. }
复制代码

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
wlt 中级黑马 2013-4-12 22:47:27
77#
public class Demo98
{
        static int count = 1; //  计数牛的个数
        //定义两个变量
        private static void feedCow(int year,int age)
{
                year++;
                age++;
                if(year<=10)
                {
                        if(age>=3)
                        {
                                count++;
                                feedCow(year,0);
                        }
                        feedCow(year,age);
                }
}
        public static void main(String[] args)
    {
                new Demo98().feedCow(0, 0);//调用方法
                System.out.println(count);
        }

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

回复 使用道具 举报
本帖最后由 燕国庆 于 2013-4-13 14:23 编辑

public class CountCow {

        /**
         * @param args
         * 一个农夫养了一头牛,三年后,这头牛每一年会生出1头牛,生出来的牛三年后,
         * 又可以每年生出一头牛……问农夫10年后有多少头牛?n年呢?(不考虑其他因素,只考虑此数学问题)


         */
        public static void main(String[] args) {
               
                //自定义几年后
                int years=10;
                //调用求N年后有多少牛的quantity()的方法
                int cattlenum=quantity(years);
                //把N年后牛的总数打印来
                System.out.println(years+"年后牛的总数目为:"+cattlenum);
               

        }
        public static int quantity(int year){
                //定义一个计数器,用来记录牛的总数
                int count=1;
                //根据题意给定循环的条件,进行循环。
                for(int i=4;i<=year;i+=3){
                        //每一次循环条件的满足都是在已有的数量的基础上进行
                        //当前数目的自加。
                        count+=count;

                }
                return count;
               
        }

}

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
本帖最后由 杜鹏飞 于 2013-4-13 18:38 编辑

推导了一个公式,目前尚未发现其他人用此方法.
  1. import java.util.*;
  2.         
  3. public class first {
  4.         private static Scanner in;
  5.         public static void main(String[] args){

  6.                 in = new Scanner(System.in);
  7.                 int N = in.nextInt();//取年数

  8.                 int[] f = new int[]{1,1,1,0};
  9.                
  10.                 for(int n=4;n<=N+1;n++){
  11.                         f[3]=f[0]+f[2];
  12.                         f[0]=f[1];
  13.                         f[1]=f[2];
  14.                         f[2]=f[3];
  15.                 }
  16.                
  17.                 System.out.println(f[3]);//输出答案
  18.         }
  19. }
复制代码

点评

如果采用面向对象的方法会更好  发表于 2013-4-14 19:43

评分

参与人数 1技术分 +2 收起 理由
田磊阳 + 2

查看全部评分

回复 使用道具 举报
  1. import java.util.ArrayList;
  2. import java.util.List;


  3. public class Cattle {

  4.         //小牛的年龄
  5.         int age=0;
  6.         //是否能生小牛
  7.         public boolean isproduce(){
  8.                 if(age>=3){
  9.                         return true;
  10.                 }else{
  11.                         return false;
  12.                 }
  13.         }
  14.         //生出来的小牛三年后才能再生产,那么age每次加++
  15.         public void addYear(){
  16.                 age++;
  17.         }
  18.         public static void main(String[] args){
  19.                 //一共几年
  20.                 int year=10;
  21.                   List catList = new ArrayList();
  22.                    //农夫的第一头母牛
  23.                   catList.add(new Cattle());
  24.                    //开始计算
  25.                    for(int i=1;i<=year;i++){
  26.                     int calf = catList.size();
  27.                     for(int j = 0; j<calf; j++){
  28.                             Cattle cattle = (Cattle) catList.get(j);
  29.                      //计算年龄 ++一次
  30.                             cattle.addYear();
  31.                      //判断一下是否到了三年能下崽了
  32.                      if(cattle.isproduce()){
  33.                       catList.add(new Cattle());
  34.                      }
  35.                     }
  36.                    }
  37.                    System.out.println("农夫"+year+"年后将有"+catList.size()+"头牛。");
  38.                 }
  39.         }
复制代码

评分

参与人数 1技术分 +3 收起 理由
田磊阳 + 3

查看全部评分

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