Java 计算N年后的奶牛数量
母牛每年生一头小牛。一头小牛在2年内变成一头牛,从一头牛开始,我们必须计算N年内有多少只动物。 假设没有牛死 例如,在N=5时: 第一头母牛产下2头小牛(1头在2岁时,另一头在3岁时,第二头在4岁时,第二头在5岁时) 总数=4头小牛 现在第一头母牛也已经3岁了,所以(她在2岁时产下一头小牛,另一头在3岁时产下) 总数=2头小牛。 第二头牛也必须是2岁,所以她刚刚生了另一头小牛 总数=1头小牛 总和=1+4+2+1 随着年复一年的增长,这一趋势仍在继续 我最近在一次考试中得到了这个问题。 我尝试过使用递归,我只是一个初学者,我没有弄对它Java 计算N年后的奶牛数量,java,recursion,Java,Recursion,母牛每年生一头小牛。一头小牛在2年内变成一头牛,从一头牛开始,我们必须计算N年内有多少只动物。 假设没有牛死 例如,在N=5时: 第一头母牛产下2头小牛(1头在2岁时,另一头在3岁时,第二头在4岁时,第二头在5岁时) 总数=4头小牛 现在第一头母牛也已经3岁了,所以(她在2岁时产下一头小牛,另一头在3岁时产下) 总数=2头小牛。 第二头牛也必须是2岁,所以她刚刚生了另一头小牛 总数=1头小牛 总和=1+4+2+1 随着年复一年的增长,这一趋势仍在继续 我最近在一次考试中得到了这个问题。 我尝试过
public static void main(String args[]) {
Scanner sc =new Scanner(System.in);
int n= sc.nextInt();
sc.close();
fun(n);
}
public static void fun(int age) {
int arr[] = new int[age-1];
int temp=0, sum=1;
for(int i=age-2; i>=0; i--){
arr[temp++]=i;
}
sum+=arr.length;
for(int j=0; j<age-1; j++) {
if(arr[j]>=2) {
fun(j);
}
}
System.out.println(sum);
}
publicstaticvoidmain(字符串参数[]){
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
sc.close();
乐趣(n);
}
公共静态无效乐趣(int age){
int arr[]=新int[age-1];
内部温度=0,总和=1;
对于(int i=age-2;i>=0;i--){
arr[temp++]=i;
}
总和+=阵列长度;
对于(int j=0;j=2){
fun(j);
}
}
系统输出打印项数(总和);
}
如前所述,该问题类似于斐波那契级数。虽然你并不局限于从一头没有小牛的奶牛开始。所以我写了一个更一般的方法:
private static int numCows(int年、int头奶牛、int头小牛){
对于(;0<年--;奶牛+=小牛,小牛=奶牛-小牛);
返回母牛;
}
或者以更为解释的方式:
private static int numCows(int年、int头奶牛、int头小牛){
对于(int i=0;i
这可能被称为这样(假设你想让奶牛在5年内有一头开始的奶牛,没有小牛):
intcows=numCows(5,1,0);
您可以试试这个。它不是递归的(这是一个严格的要求吗?),但它得到了正确的答案:
import java.util.ArrayList;
import java.util.List;
public class Bovines {
private static final class Bovine {
private final int birthYear;
public Bovine(final int birthYear) {
this.birthYear = birthYear;
}
public boolean isCow(final int year) {
return (year - this.birthYear) >= 2;
}
}
public static void main(final String[] args) throws Exception {
final List<Bovine> bovineList = new ArrayList<>();
/**/ bovineList.add(new Bovine(0)); // (initial Calf)
for (int year=0; year <= 5; year++) {
/*
* Iterate over a copy of the List to avoid any parallel-update issues...
*/
for (final Bovine bovine : new ArrayList<>(bovineList)) {
if (bovine.isCow(year)) {
bovineList.add(new Bovine(year)); // (Cow has Calf)
}
}
System.out.println("Year.: " + year + " Total: " + bovineList.size());
}
}
}
import java.util.ArrayList;
导入java.util.List;
公营牛{
私有静态最终类{
私人最后int生日;
公共牛(最后一个出生年份){
this.birthYear=生日;
}
公共布尔isCow(最后整年){
返回(年份-本年生日)>=2;
}
}
公共静态void main(最终字符串[]args)引发异常{
最终列表bovineList=新的ArrayList();
/**/牛列表。添加(新牛(0));/(初始小牛)
对于(int year=0;year可能有更有效的方法来解决这个问题,但在我看来,以下内容很容易解释和理解:
public class CowCalculator {
public static int numberOfCows(int maxAge) {
int count = 1; // the "root" cow
for (int year = 2; year <= maxAge; year++) {
// after the second year, the "root" cow gives birth to one calve every year
// every calve is a root cow by itself, but with less maximum age
count += numberOfCows(maxAge - year);
}
return count;
}
public static void main(String[] args) {
System.out.println(CowCalculator.numberOfCows(5));
}
}
公共类计算器{
公共静态int numberOfCows(int最大值){
int count=1;//根目录
对于(int year=2;year我发现了一个逻辑,但不知道如何编写程序,因为我不懂java。我不知道如何帮助你。如果你这么说,也许我可以编写一个algo。你问题的另一个维度是:如果一头牛有两头小牛:这些小牛最终会是两头牛吗?或者是一头牛和一头公牛?你可能也没有问这个问题动物和它们的兄弟姐妹交配……你提到这是一场考试——不是哪种考试。在工作面试中——即使是技术/软件工作——我也会要求澄清。在生物学中,有不同的要求。@AmanjotKaur先生,这将是非常值得赞赏的,我会尝试从我的角度暗示它。@Olafkok先生,这没有意义,但我认为墨迹就是这样。它永远是一头母牛,而不是一头公牛。@TrishaSingh你也可以试试Lino的答案