模拟摇号的小程序

简介

我做了一个关于摇号的小程序,它可以实现给n个人每个人一个不同的随机数(号码),将每个人的号码排序然后输出,并选取排名靠前一定数量的人作为摇中的,给予随机数时会有渐慢的效果。

如图:(绿色为摇中的, 红色没摇中)

 

使用方式

输入两个数 n 和 k

表示有 n 个人, 选取其中 k% 的人

代码

#include <windows.h>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <ctime>
using namespace std;

enum concol {
	black = 0,
	dark_blue = 1,
	dark_green = 2,
	dark_aqua = 3, dark_cyan = 3,
	dark_red = 4,
	dark_purple = 5, dark_pink = 5, dark_magenta = 5,
	dark_yellow = 6,
	dark_white = 7,
	gray = 8,
	blue = 9,
	green = 10,
	aqua = 11,
	cyan = 11,
	red = 12,
	purple = 13, pink = 13, magenta = 13,
	yellow = 14,
	white = 15
};

struct Peo
{
	int Num, Score;
	bool operator < (Peo b)
	{
		return Score >  b.Score;
	}
};

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
int n;
Peo a[10050];
double UpVal;
CONSOLE_CURSOR_INFO CursorInfo;
COORD _GoToPos;
int backcol, textcol;

inline void gt(short x, short y) {
	--x;
	--y;
	_GoToPos = {x, y};
	SetConsoleCursorPosition(hOut, _GoToPos);
}

int GetRand()
{
	return abs(rand() + (rand() << 17));
}

void GetRandomNum(int pln)
{
	int tmp;
	gt(5, pln);
	printf("%d", pln);
	for(int i=1;i<=20;++i)
	{
		tmp = GetRand() % 1000000;
		gt(8, pln);
		printf("%06d", tmp);
		Sleep(5);
	}
	for(int i=1;i<=15;++i)
	{
		tmp = GetRand() % 1000000;
		gt(8, pln);
		printf("%06d", tmp);
		Sleep(i*10);
	}
	a[pln].Score = tmp;
	a[pln].Num = pln;
}

inline void HideCursor()
{
	GetConsoleCursorInfo(hOut, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOut, &CursorInfo);	
}

inline void settextcolor(concol textcolor) {
	textcol = textcolor;
	unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
	SetConsoleTextAttribute(hOut, wAttributes);
}

inline void setbackcolor(concol backcolor) {
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	backcol = backcolor;
	unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
	SetConsoleTextAttribute(hOut, wAttributes);
}

int main()
{
	HideCursor();
	srand(time(NULL));
	system("mode con cols=22 lines=50");
	scanf("%d%lf", &n, &UpVal);
	system("cls");
	for(int i=1;i<=n;++i)
	{
		gt(5, i);
		printf("%d ", i);
		GetRandomNum(i);
	}
	sort(a+1, a+n+1);
	double NowVal = 0;
	for(int i=1;i<=n;++i)
	{
		NowVal = i * 100.0 / n;
		gt(5, i+n+1);
		settextcolor(white);
		setbackcolor(black);
		printf("%d ", a[i].Num);
		gt(8, i+n+1);
		printf("%06d", a[i].Score);
		if(NowVal <= UpVal)
		{
			setbackcolor(green);
		}
		else
		{
			setbackcolor(red);
		}
		printf("  ");
	}
	getchar();
	getchar();
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值