简体   繁体   English

我如何获得确切的答案?

[英]How do I get an exact answer for this?

A breeding group of 20 bighorn sheep is released in a protected area in Colorado. 在科罗拉多州的一个保护区中放养了一群20只大角羊。 It is expected that with careful management the number of sheep, N, after t years will be given by the formula: 期望通过精心管理,t年后的羊数N将由以下公式给出:

N = 220/(1 + 10(0.83)t ) and that the sheep population will be able to maintain itself without further supervision once the population reaches a size of 80. N = 220 /(1 + 10(0.83)t),并且一旦种群数量达到80只,绵羊种群将能够在没有进一步监督的情况下维持自身。

Write a program (using a for loop) that writes out the value of N for t starting at zero and going up to 25. How many years must the sheep heard be supervised? 编写一个程序(使用for循环),写出从零开始一直到25的t的N值。必须对绵羊的听到进行监督多少年?

Here is my code: 这是我的代码:

import java.lang.Math;

class SheepHerd {

public static void main(String[] args){

    for(double t = 0; t <= 25; t++) {

        double N =  220 / (1 +10 * (Math.pow(.83, t)));
        System.out.println(N);

    }
  }
}

The terminal shows me the numbers, and I can see that it takes between 9 and 10 years, but how can I get the exact years? 终端向我显示了数字,我看到它需要9到10年,但是我如何才能得到确切的年数呢?

Since you are already using a double as counter, you could try incrementing it not by 1, but by a smaller value like 0.1: 由于您已经在使用double作为计数器,因此您可以尝试不将其递增1,而应递增一个较小的值,例如0.1:

for(double t = 0; t <= 25; t+=0.1) {
        double N =  220 / (1 +10 * (Math.pow(.83, t)));
        if (N >= 80) {
            System.out.println(N + " sheep after " + t + " years");
            break;
        }

    }

This way you can adjust the granularity of time you want, but with each step you're moving into the more exact direction, computation takes longer. 这样,您可以调整所需时间的粒度,但是随着您朝着更精确的方向迈进,计算会花费更长的时间。

Add after System.out.println(N); 在System.out.println(N)之后添加;

If (N >= 80)
   break;

This will cause your program to exit the loop after the target number (80) is reached. 达到目标数(80)后,这将使您的程序退出循环。

The original problem is asking how many Years, so we can only assume it's asking for a whole number of Years. 最初的问题是询问多少年,因此我们只能假设它询问的是整数年。 At year 9, you will not have enough sheep, so it cannot be year 9. It must be year 10. 在第9年,您将没有足够的绵羊,因此不可能是第9年。它必须是第10年。

You can solve the equation for t (or let others do this for you: Wolfram alpha solution ) and implement the resulting formula: 您可以求解t的方程(或让其他人为您完成: Wolfram alpha solution )并实现结果公式:

class SheepHerd
{
    public static void main(String[] args)
    {

        for (double t = 0; t <= 25; t++)
        {
            double N = 220 / (1 + 10 * (Math.pow(.83, t)));
            System.out.println(
                "for t=" + t + " result is " + N + 
                " check: " + computeYears(N));
        }

        System.out.println(computeYears(80));
    }

    public static double computeYears(double N)
    {
        double a = Math.log(22.0 / N - 0.1);
        double b = Math.log(100.0 / 83.0);
        return -a / b;
    }
}

You could definitely invert that function. 您绝对可以反转该功能。 If my math doesn't fail me, it would be: 如果我的数学没有让我失望,那就是:

          a
N = -------------
     1 + b * c^t


       1    /  a      \
c^t = --- * | --- - 1 |
       b    \  N      /

              /                        \
       1      |    /  a      \         |
t = ------- * | lg | --- - 1 | - lg(b) |
     lg(c)    |    \  N      /         |
              \                        /

Where in your case a = 220 , b = 10 , c = .83 and get the exact value of t . 在您的情况下, a = 220b = 10c = .83并获得t的精确值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM