Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 牛的繁殖问题....??此算法如何实现.
鹏仔





发贴: 4
积分: 0
于 2005-04-02 13:04 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一只奶牛,1年可以生1只小奶牛,小奶牛4年后每一年也可以生一只奶牛 ,照此循环,;;;;;;;; 假如奶牛无死亡,20年后共有多少只奶牛?

各位大侠帮帮忙啦,小弟想了一个上午了



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
12852999





发贴: 3
积分: 0
于 2005-04-02 21:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
晕...这个问题也太简单了..


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
12852999





发贴: 3
积分: 0
于 2005-04-02 21:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我也不知道对不对...
第一代 是老羊 20;
第二代 15 14 13 12 11 10~~~~~1;
第三代 11 10 9 ~~~~~~1;
第四代 7 6 5~~~1;
第五代 3 2 1;
应该是这样了...我也不知道对不对



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
鹏仔





发贴: 4
积分: 0
于 2005-04-02 23:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
晕,好象不对咧,我开头也是这样想的

啊啊啊啊啊,高手帮帮忙啊



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
PrimeJava

一切皆有可能



发贴: 142
积分: 15
于 2005-04-03 07:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
年 1 2 3 4 5 6 7 8 9 10 11 ......
牛 2 3 4 6 9 13 19 28 41 60 88 ......

从第四年开始(包括第四年),之后的每一年的牛数目是它前一年的牛数目与前三年的牛数目之和。



I Believe I can fly!
作者 Re:牛的繁殖问题....??此算法如何实现. [Re:PrimeJava]
PrimeJava

一切皆有可能



发贴: 142
积分: 15
于 2005-04-03 08:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
年 0 1 2 3 4 5 6 7 8 9 10 11 ......
牛 1 2 3 4 6 9 13 19 28 41 60 88 ......

如果当前年是第○年,那么从第三年开始(包括第三年),之后的每一年的牛数目是它前一年的牛数目与前三年的牛数目之和。



I Believe I can fly!
作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
PrimeJava

一切皆有可能



发贴: 142
积分: 15
于 2005-04-03 12:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
001 class Cow {
002     int arraySize = 21;   // Êý×鷶Χ
003     int[] cowNumber = new int[arraySize];
004 
005     public void count(int increaseCycle, int years) {
006         cowNumber[0] = 1;
007 
008         for (int currentYear = 1; currentYear <= years; currentYear++) {
009 
010             if (currentYear < increaseCycle) {
011                 cowNumber[currentYear] = cowNumber[currentYear - 1] + 1;
012             } else {
013                 cowNumber[currentYear] = cowNumber[currentYear - 1]
014                                      + cowNumber[currentYear - increaseCycle + 1];
015             }
016             System.out.println("µÚ" + currentYear + "Ä꣺" + cowNumber[currentYear]);
017         }
018     }
019 
020     public static void main(String[] args) {
021         int increaseCycle = 4;   // ·±Ö³ÖÜÆÚ
022         int years = 20;
023         
024         Cow cow = new Cow();
025         cow.count(increaseCycle, years);
026     }
027 }


PrimeJava edited on 2005-04-03 12:56

I Believe I can fly!
作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
鹏仔





发贴: 4
积分: 0
于 2005-04-03 20:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
thank you PrimeJava
我验证一下



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
menzy



版主


发贴: 754
积分: 113
于 2005-04-04 14:00 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
递归调用而已。
但是,需不需要考虑是男牛还是女牛?



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:menzy]
鹏仔





发贴: 4
积分: 0
于 2005-04-04 23:42 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
menzy wrote:
递归调用而已。
但是,需不需要考虑是男牛还是女牛?

不需要啊,题目是这样写的

不过我还没学过递归法,不会算



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
linhaisteve





发贴: 1
积分: 0
于 2005-04-05 11:23 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
toTonguerimeJava is clever


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
2352439

学会思考



发贴: 33
积分: 0
于 2005-04-16 11:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我以前递归法学得很好,但是现在不记得了,现在只能帮你死搞---Smile

class MilchCow
{
  public static void main(String[] args)
  {
    int[][] x;
    x=new int[][]{{1,2,3,4},{9,10,11,12},{21,22,23,24},{37,38,39,40},{57,58,59,60}};
    int sum=0;
    for(int i=0;i<x.length;i++)
    {
      for(int j=0;j<x[i].length;j++)
      {
        sum+=x[i][j];
        System.out.println("x["+i+"]["+j+"]="+x[i][j]);
      }
    }
    System.out.println(sum);
  }
}

结果是530头,不知道正确不?



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
2352439

学会思考



发贴: 33
积分: 0
于 2005-04-16 19:35 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
晕,编错了程序了
偶丢脸了



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
zhengshuq





发贴: 3
积分: 0
于 2005-04-17 10:29 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好象是907头 不知道对不对


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
j2eesir





发贴: 7
积分: 0
于 2005-06-30 16:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
//一只奶牛,1年可以生1只小奶牛,小奶牛4年后每一年也可以生一只奶牛 ,照此循环,;;;;;;;;
//假如奶牛无死亡,20年后共有多少只奶牛?
public class CountBow{
public static void main(String[] args){
int[] bow=new int [21];
bow[0]=1;//第一只奶牛;bow[*]存放当年出生的牛仔

for(int year=1;year<=20;year++)
{
for(int i=0;i<=zero(year);i++)
bow[year]+=bow[i];
//输出每年新增的牛牛
System.out.println("第"+year+"年有"+bow[year]+"只奶牛出生");
}
//计算总数
int bowCount=0;
for(int year=0;year<=20;year++)
bowCount+=bow[year];
System.out.println("20年后共有"+bowCount+"只奶牛");

}
public static int zero(int year)
{
if (year-4<0)
return 0;
else
return (year-4);
}
}


j2eesir edited on 2005-06-30 16:50

作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
j2eesir





发贴: 7
积分: 0
于 2005-06-30 16:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
//编译并运行之:
第1年有1只奶牛出生
第2年有1只奶牛出生
第3年有1只奶牛出生
第4年有1只奶牛出生
第5年有2只奶牛出生
第6年有3只奶牛出生
第7年有4只奶牛出生
第8年有5只奶牛出生
第9年有7只奶牛出生
第10年有10只奶牛出生
第11年有14只奶牛出生
第12年有19只奶牛出生
第13年有26只奶牛出生
第14年有36只奶牛出生
第15年有50只奶牛出生
第16年有69只奶牛出生
第17年有95只奶牛出生
第18年有131只奶牛出生
第19年有181只奶牛出生
第20年有250只奶牛出生
20年后共有907只奶牛


j2eesir edited on 2005-06-30 16:49

作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
ljw8080





发贴: 8
积分: 0
于 2005-07-01 19:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
8错。!!!! 楼下的程序写的不错。简练!!!


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
sfox197883





发贴: 10
积分: 0
于 2005-07-05 10:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
很好啊,
for(int i=0;i<=zero(year);i++)
bow[year]+=bow[i];
这个循环求出当年可生产的牛牛数,懂了之后感觉很简单,可我就没想到



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
五个人





发贴: 7
积分: 0
于 2005-08-03 16:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
j2eesir 写得很好


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
五个人





发贴: 7
积分: 0
于 2005-08-03 16:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
j2eesir 写得很好


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
五个人





发贴: 7
积分: 0
于 2005-08-03 16:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
j2eesir 写得很好


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
naughty009





发贴: 56
积分: -2
于 2005-08-03 17:10 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Dead
头晕了



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
cxc025





发贴: 4
积分: 0
于 2005-08-04 17:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错,程序就要像sfox197883这样写


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
cxc025





发贴: 4
积分: 0
于 2005-08-04 18:06 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
年 0 1 2 3 4 5 6 7 8 9 10 11 ......
牛 1 2 3 4 5 7 10 14 19 26 36 50......

依此类推



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
truthawp

赤色彗星



发贴: 74
积分: 2
于 2005-08-08 21:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

public class Birth{
public static void main(String args[]){
int a=1;//can give birth
int b=0;//wait for 1 year
int c=0;//wait for 2 years
int d=0;//wait for 3 years
int y=0;//year
int f=0;//changable
for(y=1;y<=20;y++){
f=a;a+=b;b=c;c=d;d=f;}
System.out.println(a+b+c+d);}
}

不要笑我啊,是不是有很重的C的味道,Java类还不能运用的很熟Embaressed
不过,似乎解决这样的问题,可能个人觉得用C的思路比较快吧Confused



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
CunningFox





发贴: 3
积分: 0
于 2005-09-21 09:56 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
class Cow
{
  public void count(int year)
  {
    int y1=1,y2=0,y3=0,y4=0;
    int temp;
    for(int i=0;i<year;i++)
    {
      temp = y4;
      y4=y3;
      y3=y2;
      y2=y1;
      y1+=temp;      
    }
    System.out.println("成年羊个数: " + y1);    
    System.out.println("1年羊个数: " + y2);
    System.out.println("3年羊个数: " + y4);
    System.out.println("2年羊个数: " +y3);
    int total=0;
    total = y1 + y2 + y3 + y4;
    System.out.println("总个数: " + total);
  }
  public static void main(String[] args)
  {
    Cow cow = new Cow();
    cow.count(20);
  }
}

编着玩的,结果好像是
D:\>java Cow
成年羊个数: 345
1年羊个数: 250
3年羊个数: 131
2年羊个数: 181
总个数: 907



作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
hhxxttxs517





发贴: 1
积分: 0
于 2005-10-22 16:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
j2eesir厉害


作者 Re:牛的繁殖问题....??此算法如何实现. [Re:鹏仔]
francis36





发贴: 4
积分: 0
于 2005-10-24 21:54 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不错不错~~~



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923