Skip to content

Instantly share code, notes, and snippets.

@niklasbuschmann
Last active June 27, 2020 20:24
Show Gist options
  • Save niklasbuschmann/e963a0b925dc1e9ae75d4b662ee67bba to your computer and use it in GitHub Desktop.
Save niklasbuschmann/e963a0b925dc1e9ae75d4b662ee67bba to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <random>
using namespace std;
class normal_generator {
bool status;
double cache;
public:
normal_generator() {
status = false;
}
double random_number() {
return 2.0 * double (rand()) / RAND_MAX - 1;
}
double update() {
while (true) {
double v1 = random_number(), v2 = random_number();
double s = v1 * v1 + v2 * v2;
if (s < 1 && s != 0) {
cache = v1 * sqrt((-2 * log(s)) / s);
return v2 * sqrt((-2 * log(s)) / s);
}
}
}
double get() {
status = !status;
return status ? update() : cache;
}
};
int main() {
srand(time(NULL));
normal_generator rnd;
double m = 0, s = 0;
for (int i = 0; i < pow(10, 5); i++) {
double x = rnd.get();
m += x;
s += x*x;
}
m *= pow(10, -5);
s *= pow(10, -5);
cout << "m: " << m << endl;
cout << "σ: " << sqrt(s - m*m) << endl;
int s1 = 0, s2 = 0, s3 = 0;
for (int i = 0; i < pow(10, 5); i++) {
double x = rnd.get();
if (abs(x) > 1)
s1++;
if (abs(x) > 2)
s2++;
if (abs(x) > 3)
s3++;
}
cout << "Betrag > σ: " << s1 * pow(10, -3) << "%" << endl;
cout << "Betrag > 2σ: " << s2 * pow(10, -3) << "%" << endl;
cout << "Betrag > 3σ: " << s3 * pow(10, -3) << "%" << endl;
}
#include <iostream>
#include <vector>
using namespace std;
int num = 1;
void print(vector<int> board) {
cout << endl << "Solution " << num++ << endl;
for (int row = 0; row < board.size(); row++) {
cout << string(board[row], 'O') << "X" << string(board.size() - board[row] - 1, 'O') << endl;
}
}
bool check(int row, int col, vector<int> board) {
for (int i = 0; i < row; i++) {
if (board[i] == col || board[i] == col + (row - i) || board[i] == col - (row - i))
return false;
}
return true;
}
void place(int row, vector<int> board) {
if (row == board.size())
return print(board);
for (int col = 0; col < board.size(); col++) {
if (check(row, col, board)) {
board[row] = col;
place(row + 1, board);
}
}
}
int main () {
vector<int> board(8, 0);
place(0, board);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment