2020秋資料結構實驗第一題:兔子數列模擬(斐波那契數列模擬)C++實現
實驗題目描述:
- 程式設計實現兔⼦問題,歷史上有⼀個有名的關於兔⼦的問題:假設有⼀對兔⼦,⻓兩個⽉它們就算 ⻓⼤成年了。然後以後每個⽉都會⽣出1對兔⼦,⽣下來的兔⼦也都是⻓兩個⽉就算成年,然後每 個⽉也都會⽣出1對兔⼦了。這⾥假設兔⼦不會死,每次都是隻⽣1對兔⼦,實現計算第20個(第 15)⽉的兔⼦對數
- 加入條件,每對兔⼦⽣完2次(N次)兔⼦後會死亡,實現計算第20個(第15)⽉的兔⼦對數
- 每隔⼏個⽉可以繁殖設定為引數K, 繁殖⼏代後死亡設定成引數P,計算第N個⽉的兔⼦對數
題目分析:
看似題目是三個逐步遞進的過程,其實還是比較簡單的,只需要建立一個簡單的結構體或者類,,然後根據相應的標誌位設定是否死亡,是否長大,是否新生,是否可繁殖這四步,主要的其實是兩個動作,一個出生,一個死亡,就是結構體陣列的刪除和新建罷了,後面放出原始碼,未加入繁殖兩代之間的間隔,可自行加入,大家僅供參考,不可原版照抄!
題目實現原始碼
#include<iostream>
#include<stdio.h>
#include<vector>
#include<String>
using namespace std;
class rabbit {
private:
bool Is_Adult;
bool Is_Death;
int Grown_Time;
int Breed_Time;
int DEATH_TIME;
int MATURE_TIME;
public:
rabbit(int Death_Time, int Mature_Time) {
DEATH_TIME = Death_Time;
MATURE_TIME = Mature_Time;
Is_Adult = false;
Is_Death = false;
Grown_Time = 0;
Breed_Time = 0;
}
bool Get_Is_Adult() {
return Is_Adult;
}
bool Get_Is_Death() {
return Is_Death;
}
int Get_Grown_Time() {
return Grown_Time;
}
int Get_Breed_Time() {
return Breed_Time;
}
void Add_Grown_Time() {
Grown_Time++;
if (Grown_Time >= MATURE_TIME)
Is_Adult = true;
}
void Add_Breed_Time() {
Breed_Time++;
if (Breed_Time >= DEATH_TIME)
Is_Death = true;
}
};
int Compute_Num(int Month_Num, vector<rabbit>Rabbits, int Death_Time, int Mature_Time) {
for (int i = 0; i < Month_Num; i++)
{
int Rabbits_Size_Now = Rabbits.size();
int Dead_Rabbits = 0;
int Born_Rabbits = 0;
for (int j = 0; j < Rabbits_Size_Now;)
{
if (Rabbits[j].Get_Is_Death())
{
Rabbits.erase(Rabbits.begin() + j);
Rabbits_Size_Now--;
Dead_Rabbits++;
continue;
}
if (Rabbits[j].Get_Is_Adult()) {
rabbit *r = new rabbit(Death_Time, Mature_Time);
Rabbits.push_back(*r);
Rabbits[j].Add_Breed_Time();
Born_Rabbits++;
Rabbits_Size_Now++;
if (Rabbits[j].Get_Is_Death())
{
Rabbits.erase(Rabbits.begin() + j);
Rabbits_Size_Now--;
Dead_Rabbits++;
continue;
}
}
else
Rabbits[j].Add_Grown_Time();
j++;
}
printf("The %2d month has %2d rabbits!\n",i+1,Rabbits.size());
if (Dead_Rabbits)
printf("The %2d month has %2d rabbits dead!\n", i+1,Dead_Rabbits);
if (Born_Rabbits)
printf("The %2d month has %2d rabbits born!\n", i+1, Born_Rabbits);
//cout << "The " << i + 1 << " month has " << Rabbits.size() << " rabbits!" << endl;
}
return Rabbits.size();
}
int main() {
int Death_Time, Mature_Time, Month_Num;
char s[200] = "Example: Death_Time = 100000000, Mature_Time = 2, Month_Num = 12";
cout << "/";
for (int i = 0; i < strlen(s); i++)
cout << "*";
cout << "/"<<endl;
cout << s << endl;
cout << "/";
for (int i = 0; i < strlen(s); i++)
cout << "*";
cout << "/";
cout << endl;
char s1[100] = "You will get a fibonacci sequence!";
cout << s1 << endl;
cout << "/";
for (int i = 0; i < strlen(s); i++)
cout << "*";
cout << "/";
cout << endl;
while (1) {
cout << "Please input Death_Time: ";
cin >> Death_Time;
while (cin.fail()) {
cout << "Wrong, you have inputed a wrong type data!" << endl;
cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
/*cin.sync();*///清楚cin快取區的資料。
while (cin.get() != '\n') {
continue;
}
cout << "please input Death_Time again" << endl;
cin >> Death_Time;
}
cout << "Please input Mature_Time: ";
cin >> Mature_Time;
while (cin.fail()) {
cout << "Wrong, you have inputed a wrong type data!" << endl;
cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
/*cin.sync();*///清楚cin快取區的資料。
while (cin.get() != '\n') {
continue;
}
cout << "please input Mature_Time again" << endl;
cin >> Mature_Time;
}
cout << "Please input Month_Num to Compute: ";
cin >> Month_Num;
while (cin.fail()) {
cout << "Wrong, you have inputed a wrong type data!" << endl;
cin.clear();//清除錯誤標記,重新開啟輸入流,但是輸入流中依舊保留著之前的不匹配的型別
/*cin.sync();*///清楚cin快取區的資料。
while (cin.get() != '\n') {
continue;
}
cout << "please input Month_Num again" << endl;
cin >> Month_Num;
}
vector<rabbit>Test;
rabbit* a = new rabbit(Death_Time, Mature_Time);
Test.push_back(*a);
int rabbit_sum_num = Compute_Num(Month_Num, Test, Death_Time, Mature_Time);
}
}
相關文章
- 資料結構之斐波那契數列java實現資料結構Java
- js實現斐波那契數列JS
- 斐波那契數列js 實現JS
- 斐波那契數列
- 使用Python實現斐波那契數列Python
- JavaScript 實現:輸出斐波那契數列JavaScript
- js迭代器實現斐波那契數列JS
- 斐波那契數列的python實現Python
- 斐波那契數列(Java)Java
- 斐波那契數列三種實現函式函式
- 斐波那契數列演算法 JS 實現演算法JS
- noip模擬9[斐波那契·數顏色·分組](洛谷模擬測試)
- 斐波那契數列 (C#)C#
- PHP 與斐波那契數列PHP
- 斐波那契數列詳解
- 斐波那契數列 多語言實現 筆記筆記
- 面試題9-斐波那契數列面試題
- Python 實現 動態規劃 /斐波那契數列Python動態規劃
- 斐波那契數列演算法演算法
- 斐波那契數列Ⅳ【矩陣乘法】矩陣
- 裴波那契數列(javascript實現)JavaScript
- 資料結構之C語言模擬整數陣列實現資料結構C語言陣列
- 斐波那契數列的遞迴和非遞迴實現遞迴
- 演算法(1)斐波那契數列演算法
- [C103] 斐波那契數列
- 大數斐波那契數列的演算法演算法
- 斐波那契數
- 每日一算 -- 斐波那契數列型別題型別
- 演算法一:斐波那契阿數列演算法
- 斐波那契數列的分治法計算
- 斐波那契數列(Fibonacci)遞迴和非遞迴實現遞迴
- rust實戰系列 - 使用Iterator 迭代器實現斐波那契數列(Fibonacci )Rust
- “斐波那契數列”問題的遞推演算法演算法
- 計算斐波那契數列的演算法演算法
- 劍指offer-9-斐波那契數列-javaJava
- hdu 3117矩陣+斐波那契數列矩陣
- HITOJ 2060 類似斐波那契數列(一段和取模)
- Golang利用select和普通函式分別實現斐波那契數列Golang函式