⭐ Article link: www.mengyingjie.com/archives/37/ ⭐

## Dolphin baby in Ctrip Aquarium

### Title Description:

There are n cute dolphins in Ctrip aquarium. They are all 0 years old at the beginning, and the life span of each dolphin is m years,

And these little dolphins will give birth to a baby dolphin (1 < = birthyear [i] < = m) in birthyear [i], and each baby dolphin is 0 years old.

How many dolphins were there in Ctrip aquarium in X years?

#### input

N (initial number of dolphins)

M (dolphin life)

Number of years in which dolphins have babies (assuming P)

Year of dolphin birth 1

…

Year of birth P

X (after a few years)

#### output

How many dolphins will there be in X years

#### sample input

5

5

2

2

4

5

#### sample output

20

### code

```
#include <iostream>
using namespace std;
int main()
{
//N dolphin, dolphin life span m, number of years in which dolphins have babies P, x years later
int n, m, p, x;
//N1 refers to the existing dolphins, N2 refers to the dolphins that have existed together, including the dead ones, so the array size is defined
int n1,n2;
//Arr2 indicates how many years the dolphins came into the world, and the dead dolphins are - 1, which is defined as large enough to store
int arr2[99999];
cin >> n >> m >> p;
n1 = n2 = n ;
//Arr1 to store the year of birth
int arr1[p];
//Enter the year of birth
for(int i = 0; i < p; i++){
cin >> arr1[i];
}
cin >> x;
//Initialize the first few years of dolphins coming into the world. Initialize to 1, indicating the first year
for(int i = 0; i < n; i++){
arr2[i] = 1;
}
//Enter the year cycle, X cycles, indicating the past x years
for(int i = 1; i <= x; i++){
//The year of each year cycle
for(int j = 0; j < n2; j++){
//If the dolphin's year is - 1, it means it has died
if(arr2[j] == -1)continue;
//The year in which the dolphin gave birth
for(int k = 0; k < p; k++){
//If so
if(arr2[j] == arr1[k]){
n1++;
n2++;
arr2[n2-1] = 0;
}
}
//When the life is up, it is set to - 1. Because the initial age is 0, the age is equal to the year of existence - 1
if(arr2[j] - 1 == m){
n1--;
arr2[j] = -1;
}
arr2[j]++;
}
}
cout << n1 << endl;
return 0 ;
}
```

Such problems are encountered, but they are still not solved after reading the article,

Comment or add QQ: 781378815