URL 已经复制到剪切板

查看 gist

查看一个gist的详细信息

简介

养猪场示例

名称

pigs.cpp

创建者

doujiang_zheng2

创建时间

2017-12-18 11:07

最后修改

2017-12-18 11:07

代码
#include <windows.h> #include <iostream> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; const int BIG_WHITE = 0; const int LITTLE_BLACK = 1; // 楼上向阳 int type_upstair[50][5]; double weight_upstair[50][5]; double speed_upstair[50][5]; int feed_days_upstair[50][5]; bool is_sold_upstair[50][5]; // 楼下背阴 int type_downstair[50][4]; double weight_downstair[50][4]; double speed_downstair[50][4]; int feed_days_downstair[50][4]; bool is_sold_downstair[50][4]; void rand_sun_speed() { for (int i = 0; i < 50; i++) for (int j = 0; j < 5; j++) if (is_sold_upstair[i][j]) // 向阳的生长速率为0.2-1.2 speed_upstair[i][j] = rand() / double(RAND_MAX) + 0.2; } void rand_shade_speed() { for (int i = 0; i < 50; i++) for (int j = 0; j < 4; j++) if (is_sold_upstair[i][j]) // 背阴的生长速率为0.0-1.0 speed_downstair[i][j] = rand() / double(RAND_MAX); } void init_sun() { for (int i = 0; i < 50; i++) for (int j = 0; j < 5; j++) if (is_sold_upstair[i][j]) { type_upstair[i][j] = rand() % 2; weight_upstair[i][j] = rand() / double(RAND_MAX) * 30 + 20; } } void init_shade() { for (int i = 0; i < 50; i++) for (int j = 0; j < 4; j++) if (is_sold_upstair[i][j]) { type_downstair[i][j] = rand() % 2; weight_downstair[i][j] = rand() / double(RAND_MAX) * 30 + 20; } } void add_weight_day_by_day() { for (int i = 0; i < 50; i++) for (int j = 0; j < 5; j++) { weight_upstair[i][j] += speed_upstair[i][j]; feed_days_upstair[i][j]++; } for (int i = 0; i < 50; i++) for (int j = 0; j < 4; j++) { weight_downstair[i][j] += speed_downstair[i][j]; feed_days_downstair[i][j]++; } } void sell_pigs() { int sum = 0; int price[2] = {7, 14}; for (int i = 0; i < 50; i++) { for (int j = 0; j < 5; j++) { cout << weight_upstair[i][j] << ' '; if (weight_upstair[i][j] * 2 > 150 || feed_days_upstair[i][j] > 180) { sum += 2 * price[type_upstair[i][j]] * weight_upstair[i][j]; feed_days_upstair[i][j] = 0; weight_upstair[i][j] = 0; speed_upstair[i][j] = 0; is_sold_upstair[i][j] = true; } } cout << endl; } for (int i = 0; i < 50; i++) { for (int j = 0; j < 4; j++) { cout << weight_downstair[i][j] << ' '; if (weight_downstair[i][j] * 2 > 150 || feed_days_downstair[i][j] > 180) { sum += 2 * price[type_downstair[i][j]] * weight_downstair[i][j]; feed_days_downstair[i][j] = 0; weight_downstair[i][j] = 0; speed_downstair[i][j] = 0; is_sold_downstair[i][j] = true; } } cout << endl; } cout << "本次卖猪总收入" << sum << endl; } int main() { memset(is_sold_upstair, 1, sizeof(is_sold_upstair)); memset(is_sold_downstair, 1, sizeof(is_sold_downstair)); cout << "开始养猪" << endl; srand((unsigned)time(NULL)); for (int days = 0; days <= 365; days++) { Sleep(300); if (days > 0) cout << "Days: " << days << ' '; if (days % 10 == 0) cout << endl; if (days > 0 && days % 90 == 0) sell_pigs(); init_sun(); rand_sun_speed(); init_shade(); rand_shade_speed(); for (int i = 0; i < 50; i++) { for (int j = 0; j < 5; j++) is_sold_upstair[i][j] = false; for (int j = 0; j < 4; j++) is_sold_downstair[i][j] = false; } add_weight_day_by_day(); } return 0; }
分享