Algorithm programming (Java) # The cow calf problem [bytes]

Copyright: Author: xp_9512 Source: CSDN Copyright: This article is a blogger original article, reproduced, please attach Bowen link! Original: https://blog.csdn.net/qq_40981804/article/details/89839746

Title Description

From the beginning of cows 3-7 years old will have a calf every year, after the death of 10-year-old (10 years old survival). The initial investment is assumed that there is a cow born 1 year n ask how many head of cattle? (Counting from the first year)

Note: The third year will be the first-born cows, so the third year have two cows.

Fifth year, the third year of birth of the cows will produce, so the first five years have five cows.

Age is imaginary

input Output

  • Enter the number of years n
  • Number of cattle output

In the first cow 1 has
a second cow 1 there were
third year of birth of a cow, the cow 2 it has
a fourth of a cow to be born, so there are three cows
fifth year ..., so cows 5
The first 12 years ..., it has 123 head of cattle

Thinking

  • Creating a cow objects, properties, only age
  • Create a collection class, an object used to store cattle
  • Traverse this collection
  • Determining whether the bovine growth, then it can be added in a further set of objects
  • Determine whether the cattle deaths, it is removed from the collection, or age, plus one
  • The number of output object in the collection

Stepped pit

Code

package com.xp;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        //输入所要计算的年
        Scanner sc = new Scanner(System.in);
        //测试用 存放每只牛的年龄
        List<Integer> rm = new ArrayList<>();
        //定义一个集合存放牛的数量
        List<Animal> m = new ArrayList<>();
        m.add(new Animal(1));
        //判断查询哪一年的数量
        int n = sc.nextInt();
        for(int i = 1;i<= n;i++){

            for(int j = 0 ;j < m.size();j++){
                Animal animal = m.get(j);
                //看看牛是否可以生育
                if(animal.getAge()>= 3 && animal.getAge()<=7){
                    m.add(new Animal(1));
                }
                //判断牛是否死亡
                if(animal.getAge()==10){
                    m.remove(j);
                }else {
                    //年龄加
                    animal.setAge(animal.getAge() + 1);
                }
            }
            rm.add(m.size());
            //测试用输出当年牛的年龄
//            if(i==n){
//                Iterator it = m.iterator();
//                while (it.hasNext()) {
//                    Animal animal = (Animal)it.next();
//                    System.out.println(animal.getAge()-1);
//                }
//            }
        }

        //输出牛的数量
        System.out.println(m.size());
    }


}
//定义母牛这个类
 class Animal{
    private int age;

    public Animal(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}








Guess you like

Origin blog.csdn.net/qq_40981804/article/details/89839746