Skip to content

Instantly share code, notes, and snippets.

@niklasbuschmann
Last active May 23, 2020 18:54
Show Gist options
  • Save niklasbuschmann/742fee8392c1265e923f2b19ca21bd48 to your computer and use it in GitHub Desktop.
Save niklasbuschmann/742fee8392c1265e923f2b19ca21bd48 to your computer and use it in GitHub Desktop.
#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;
}
#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