简介
我做了一个关于摇号的小程序,它可以实现给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;
}