假设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(
}