Last active
May 23, 2020 18:54
-
-
Save niklasbuschmann/742fee8392c1265e923f2b19ca21bd48 to your computer and use it in GitHub Desktop.
This file contains 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 <iostream> | |
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
#include <random> | |
using namespace std; | |
double random_number() { | |
return double (rand()) / RAND_MAX; | |
} | |
double prob(int n) { | |
int h = 0, i = n; | |
while (i--) { | |
double x = random_number(), y = random_number(); | |
h += sqrt(x * x + y * y) <= 1; | |
} | |
return double(h) / double(n); | |
} | |
int main () { | |
int n; | |
srand(time(NULL)); | |
cout << "n: "; | |
cin >> n; | |
cout << "pi: " << prob(n) * 4 << endl; | |
} |
This file contains 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 <iostream> | |
#include <iomanip> | |
#include <fstream> | |
#include <cstdlib> | |
#include <sstream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int main() { | |
vector<string> teams; | |
vector<int> sums; | |
string line; | |
ifstream file("a11-toredat.sec"); | |
while (getline(file, line)) { | |
int n, id, sum = 0; | |
istringstream stream(line); | |
stream >> id; | |
string team = "id: " + to_string(id) + " goals: "; | |
while (stream >> n) { | |
team += to_string(n) + " "; | |
sum += n; | |
} | |
teams.push_back(team); | |
sums.push_back(sum); | |
} | |
for (int i = 0; i < teams.size(); i++) | |
cout << teams[i] << " sum: " << sums[i] << ((sums[i] == *max_element(sums.begin(), sums.end())) ? " champion" : "") << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment