假设每个人每秒,将一块饼干随机给其他人

假设20个小朋友,每个人有100个饼干
每个小朋友每秒给出一个饼干,随机给其他小朋友
直到有小朋友一个饼干也没有请添加图片描述

#include<graphics.h>
#include<iostream>
using namespace std;
#pragma warning (disable:4996)
#define size 20

int arry[size];
char name[size];
int MAX;

// 是否有人破产
bool breakup()
{
	for (int i = 0; i < size; i++)
	{
		if (arry[i] == 0)
		{
			return 0;
		}
	}
	return 1;
}

// 资产交换
void MoneyChange()
{
	for (int i = 0; i < size; i++)
	{
		arry[i]--;
		arry[rand() % size]++;
	}
	MAX++;
}

// 降序排列
void Draw1()
{
	char a[size];
	int Temparry[size];
	int MAX = 0;
	for (int i = 0; i < size; i++)
	{
		Temparry[i] = arry[i]; a[i] = name[i];
	}

	for (int i = size-1; i>=0; i--)
	{
		for (int j =0; j<i; j++)
		{
			if (Temparry[j] < Temparry[j + 1])
			{
				swap(a[j], a[j + 1]);
				swap(Temparry[j], Temparry[j + 1]);
			}
		}
	}
	
	int eche_width=(2240 / 4)  / size;
	int eche_interval = eche_width;
	eche_width = (eche_width / 3) * 2;
	int left=80;
	int bottom = 1330 / 4 + 80;

	for (int i = 0; i < size; i++)
	{
		fillrectangle(left, bottom - (Temparry[i]*2), left+eche_width, bottom);
		char temp[20];
		sprintf(temp, "%c",a[i]);
		outtextxy(left, bottom+2,*temp);
		left = left + eche_interval;
	}
	return;
}

// 正常显示
void Draw2()
{
	int eche_width = (2240 / 4) / size;
	int eche_interval = eche_width;
	eche_width = (eche_width / 3) * 2;
	int left = (2240 / 2)-250;
	int bottom = 1330 / 4 + 80;

	for (int i = 0; i < size; i++)
	{
		fillrectangle(left, bottom - (arry[i] * 2), left + eche_width, bottom);
		char temp[20];
		sprintf(temp, "%c", name[i]);
		outtextxy(left, bottom + 2, *temp);
		left = left + eche_interval;
	}
}

// 山形图
void Draw3()
{

	char a[size];
	int Temparry[size];
	int Temparry1[size]={0};
	for (int i = 0; i < size; i++)
	{
		a[i] = name[i]; Temparry[i] = arry[i];
	}
	for (int i = size - 1; i >= 0; i--)
	{
		for (int j = 0; j < i; j++)
		{
			if (Temparry[j] < Temparry[j + 1])
			{
				swap(a[j], a[j + 1]);
				swap(Temparry[j], Temparry[j + 1]);
			}
		}
	}
	int site = size/2;
	for (int i = 0; i < size; i++)
	{
		site = (i % 2) == 0 ? site + i : site - i;
		Temparry1[site]=Temparry[i];
	}


	int eche_width = (2240 / 4) / size;
	int eche_interval = eche_width;
	eche_width = (eche_width / 3) * 2;
	int left = (2240 / 2) - 250;
	int bottom = 890;

	for (int i = 0; i < size; i++)
	{
		fillrectangle(left, bottom - (Temparry1[i] * 2), left + eche_width, bottom);
		char temp[20];
		sprintf(temp, "%c", a[i]);
		outtextxy(left, bottom + 2, *temp);
		left = left + eche_interval;
	}
}

// 升序排列
void Draw4()
{
	char a[size];
	int Temparry[size];
	int MAX = 0;
	for (int i = 0; i < size; i++)
	{
		Temparry[i] = arry[i]; a[i] = name[i];
	}

	for (int i = size; i > 0; i--)
	{
		for (int j = 0; j < i; j++)
		{
			if (Temparry[j] < Temparry[j + 1])
			{
				swap(a[j], a[j + 1]);
				swap(Temparry[j], Temparry[j + 1]);
			}
		}
	}

	int eche_width = (2240 / 4) / size;
	int eche_interval = eche_width;
	eche_width = (eche_width / 3) * 2;
	int left = 80;
	int bottom = 890;

	for (int i = (size-1); i >=0; i--)
	{
		fillrectangle(left, bottom - (Temparry[i] * 2), left + eche_width, bottom);
		char temp[20];
		sprintf(temp, "%c", a[i]);
		outtextxy(left, bottom + 2, *temp);
		left = left + eche_interval;
	}
}



int main()
{
	for (int i = 0; i < size; i++)
	{arry[i] = 100;}
	for (int i = 0; i < size; i++)
	{name[i] = (char)('a' + i); }

	HWND window = initgraph(2240, 1330);
	ShowWindow(window, SW_MAXIMIZE);
	setbkcolor(WHITE);
	cleardevice();
	setcolor(RED);
	setfillcolor(RED);

	while (breakup())
	{
		MoneyChange();
		
		cleardevice();
		BeginBatchDraw();
		Draw1();
		Draw2();
		Draw3();
		Draw4();
		FlushBatchDraw();
		//Sleep(10);

		

		
	}
	// 山形表,正序表,原序表,
	char a[10]={""};
	sprintf(a, "%d",MAX);
	
	
	WCHAR wszClassName[256];
	memset(wszClassName, 0, sizeof(wszClassName));
	MultiByteToWideChar(CP_ACP, 0, a, strlen(a) + 1, wszClassName,
		sizeof(wszClassName) / sizeof(wszClassName[0]));

	MessageBox(NULL, wszClassName,TEXT("总分配次数"),0);
	getchar();
	closegraph();

	//void fillrectangle(
	//void outtextxy(

	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个典型的贪心算法问题。假设有m个孩子和n个饼干,每个孩子都有一个贪心因子g[i],每个饼干都有一个大小s[j]。我们需要将饼干分配给孩子,使得满足贪心条件的孩子个数最大。具体的思路如下: 1. 将孩子和饼干按照贪心因子和大小从小到大排序。 2. 从第一个孩子和第一个饼干开始,依次比较孩子的贪心因子和饼干的大小,如果饼干的大小可以满足孩子的贪心因子,就将这个饼干分配给这个孩子,并且继续考虑下一个孩子和下一个饼干;否则就考虑下一个饼干是否可以满足这个孩子的贪心因子,直到找到一个合适的饼干或者没有合适的饼干为止。 3. 继续考虑下一个孩子和下一个饼干,直到所有的孩子都被分配了饼干或者没有合适的饼干为止。 下面是一个可运行的C++代码实现: ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int findContentChildren(vector<int>& g, vector<int>& s) { sort(g.begin(), g.end()); sort(s.begin(), s.end()); int i = 0, j = 0; while (i < g.size() && j < s.size()) { if (g[i] <= s[j]) { i++; } j++; } return i; } int main() { vector<int> g = {1, 2, 3}; vector<int> s = {1, 1}; cout << findContentChildren(g, s) << endl; // 输出1 g = {1, 2}; s = {1, 2, 3}; cout << findContentChildren(g, s) << endl; // 输出2 return 0; } ``` 其中,g为贪心因子数组,s为饼干大小数组,函数返回可以满足的孩子个数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值