Created
September 29, 2015 22:37
-
-
Save thypon/18ca1fe6a038ffeac62e to your computer and use it in GitHub Desktop.
wheel extraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <tuple> | |
#include <list> | |
#include <string> | |
#include <cstdlib> | |
#include <ctime> | |
#include <iostream> | |
#include <thread> | |
#define ENTRY pair<string, unsigned int> | |
#define LABEL 0 | |
#define WEIGHT 1 | |
using namespace std; | |
int main(int argc, char const *argv[]) { | |
list<ENTRY> l; | |
int weight, max = 0; | |
string label; | |
srand(time(0)); | |
for (cin >> weight; weight > 0; cin >> weight) { | |
max += weight; | |
cin >> label; | |
l.push_back(make_pair(label, weight)); | |
} | |
int extract = rand() % max + 1; | |
cout << extract << endl; | |
list<ENTRY>::iterator it = l.begin(); | |
extract -= get<WEIGHT>(*it); | |
while (extract > 0) { | |
extract -= get<WEIGHT>(* ++it); | |
} | |
cout << get<LABEL>(*it) << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment