模拟摇号的小程序
簡介
我做了一個關于搖號的小程序,它可以實現給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; }總結
- 上一篇: STM32 LED灯的另一种写法
- 下一篇: 51单片机——SPI