Initial commit
This commit is contained in:
80
cpp/programs/knapsack_rewritable_array.cpp
Normal file
80
cpp/programs/knapsack_rewritable_array.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
|
||||
int getMaxValue(int P[], int W[], int C, int N) {
|
||||
int table[2][C] = {0};
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < C; j++) {
|
||||
table[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
int *topRow;
|
||||
int *bottomRow;
|
||||
if (i % 2 == 0) {
|
||||
topRow = table[0];
|
||||
bottomRow = table[1];
|
||||
} else {
|
||||
topRow = table[1];
|
||||
bottomRow = table[0];
|
||||
}
|
||||
|
||||
|
||||
if (1 >= W[i]) {
|
||||
bottomRow[0] = max(P[i] + topRow[1 - W[i]], bottomRow[1 - 1]);
|
||||
} else {
|
||||
bottomRow[0] = bottomRow[1 - 1];
|
||||
}
|
||||
|
||||
for (int w = 1; w < C; w++) {
|
||||
if (w + 1 > W[i]) {
|
||||
bottomRow[w] = max(P[i] + topRow[(w + 1) - W[i]], topRow[w]);
|
||||
} else {
|
||||
bottomRow[w] = topRow[w];
|
||||
}
|
||||
|
||||
for (int y = 0; y < 2; y++) {
|
||||
for (int x = 0; x < C; x++) {
|
||||
cout << table[y][x] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
// string tmp;
|
||||
// cin >> tmp;
|
||||
}
|
||||
}
|
||||
|
||||
return max(table[0][C-1], table[1][C-1]);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
|
||||
int N, C;
|
||||
input_stream >> N;
|
||||
input_stream >> C;
|
||||
|
||||
int P[N];
|
||||
int W[N];
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
input_stream >> W[i];
|
||||
input_stream >> P[i];
|
||||
}
|
||||
|
||||
cout << getMaxValue(P, W, C, N) << endl;
|
||||
|
||||
input_stream.close();
|
||||
return 0;
|
||||
}
|
||||
100
cpp/programs/quick_primes.cpp
Normal file
100
cpp/programs/quick_primes.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int start, end;
|
||||
|
||||
cin >> start;
|
||||
cin >> end;
|
||||
// const int start = 1;
|
||||
// const int end = 1000000;
|
||||
|
||||
|
||||
|
||||
list<int> primes;
|
||||
|
||||
primes.push_back(1);
|
||||
primes.push_back(2);
|
||||
primes.push_back(3);
|
||||
primes.push_back(5);
|
||||
|
||||
// Fill all numbers, excluding most obvious non-primes numbers
|
||||
for (int i = 6; i <= end; i++) {
|
||||
if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) {
|
||||
continue;
|
||||
}
|
||||
primes.push_back(i);
|
||||
}
|
||||
|
||||
auto it = primes.begin();
|
||||
advance(it, 4);
|
||||
|
||||
int stop = sqrt(end);
|
||||
for (it; *it <= stop; it++) {
|
||||
/*
|
||||
Go through all numbers in the list that are lower than sqrt(end)
|
||||
The list is modified in the loop, allowing to skip processing of some numbers
|
||||
*/
|
||||
int factor = *it;
|
||||
|
||||
auto it2 = it;
|
||||
it2++;
|
||||
|
||||
auto stop2 = primes.end();
|
||||
for (it2; it2 != stop2; it2++) {
|
||||
/*
|
||||
Go through all numbers that are greater than the *it,
|
||||
removing them if they can be divided by *it
|
||||
*/
|
||||
|
||||
if (*it2 % factor == 0) {
|
||||
auto tmp = next(it2);
|
||||
|
||||
primes.erase(it2);
|
||||
|
||||
it2 = prev(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A dumb way to handle end < 5
|
||||
if (end < primes.back()) {
|
||||
auto trueEnd = primes.begin();
|
||||
auto stop = primes.end();
|
||||
while (trueEnd != stop && *trueEnd <= end) {
|
||||
trueEnd++;
|
||||
}
|
||||
primes.erase(trueEnd, stop);
|
||||
}
|
||||
|
||||
|
||||
auto startIt = primes.end();
|
||||
auto primesEnd = primes.end();
|
||||
|
||||
it = primes.begin();
|
||||
for (it; it != primesEnd; it++) {
|
||||
if (*it >= start) {
|
||||
startIt = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (start == end && *startIt != start) {
|
||||
// Range contains only one number and it's not prime
|
||||
cout << "Absent" << endl;
|
||||
} else if (it == primesEnd) {
|
||||
cout << "Absent" << endl;
|
||||
} else {
|
||||
it = startIt;
|
||||
for (it; it != primesEnd; it++) {
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
98
cpp/programs/random.cpp
Normal file
98
cpp/programs/random.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
typedef mt19937 MyRNG;
|
||||
// uint32_t seed_val;
|
||||
|
||||
MyRNG rng;
|
||||
bool rngInitialized = false;
|
||||
|
||||
const string ALPHABET = "abcdefghijklmnopqrstuvwxyz ";
|
||||
const string DESIRED_STRING = "methinks it is like a weasel";
|
||||
|
||||
|
||||
void initialize_random() {
|
||||
rng.seed(time(0));
|
||||
}
|
||||
|
||||
|
||||
int randint(int start, int end) {
|
||||
if (!rngInitialized) {
|
||||
initialize_random();
|
||||
rngInitialized = true;
|
||||
}
|
||||
|
||||
uniform_int_distribution<uint32_t> myDist(start, end);
|
||||
return myDist(rng);
|
||||
}
|
||||
|
||||
|
||||
string generate_string(string seed) {
|
||||
string result;
|
||||
for (int i = 0; i < DESIRED_STRING.length(); i++) {
|
||||
if (seed[i] == DESIRED_STRING[i]) {
|
||||
result = result + seed[i];
|
||||
} else {
|
||||
result = result + ALPHABET[randint(0, 26)];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
string generate_string() {
|
||||
string seed;
|
||||
for (int i = 0; i < DESIRED_STRING.length(); i++) {
|
||||
seed = seed + '-';
|
||||
}
|
||||
return generate_string(seed);
|
||||
}
|
||||
|
||||
|
||||
int appraise_string(string str) {
|
||||
if (str == DESIRED_STRING) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
int correct = 0;
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
if (str[i] == DESIRED_STRING[i]) {
|
||||
correct++;
|
||||
}
|
||||
}
|
||||
|
||||
return (correct * 100) / DESIRED_STRING.length();
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
string res = generate_string();
|
||||
int current_attempt = appraise_string(res);
|
||||
int best_attempt = current_attempt;
|
||||
string best_attempt_str = res;
|
||||
|
||||
int counter = 0;
|
||||
while(current_attempt != 100) {
|
||||
res = generate_string(best_attempt_str);
|
||||
current_attempt = appraise_string(res);
|
||||
|
||||
if (current_attempt > best_attempt) {
|
||||
best_attempt = current_attempt;
|
||||
best_attempt_str = res;
|
||||
}
|
||||
|
||||
if (counter == 1000) {
|
||||
cout << "Current best attempt with " << best_attempt << "%: " << best_attempt_str << endl;
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
cout << "Generated result with counter at " << counter << endl;
|
||||
return 0;
|
||||
}
|
||||
92
cpp/programs/sort_merge.cpp
Normal file
92
cpp/programs/sort_merge.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
void merge(int array[], int left, int mid, int right) {
|
||||
int leftArrLen = mid - left + 1;
|
||||
int rightArrLen = right - mid;
|
||||
|
||||
// Create temp arrays
|
||||
int leftArray[leftArrLen];
|
||||
int rightArray[rightArrLen];
|
||||
|
||||
// Copy data to temp arrays leftArray[] and rightArray[]
|
||||
for (int i = 0; i < leftArrLen; i++) {
|
||||
leftArray[i] = array[left + i];
|
||||
}
|
||||
|
||||
for (auto i = 0; i < rightArrLen; i++) {
|
||||
rightArray[i] = array[mid + 1 + i];
|
||||
}
|
||||
|
||||
int leftArrIndex = 0; // Initial index of first sub-array
|
||||
int rightArrIndex = 0; // Initial index of second sub-array
|
||||
int mergedArrIndex = left; // Initial index of merged array
|
||||
|
||||
// Merge the temp arrays back into array[left..right]
|
||||
while (leftArrIndex < leftArrLen && rightArrIndex < rightArrLen) {
|
||||
// Choose one of the two elements at current positions of left and right arrays
|
||||
if (leftArray[leftArrIndex] <= rightArray[rightArrIndex]) {
|
||||
array[mergedArrIndex] = leftArray[leftArrIndex];
|
||||
leftArrIndex++;
|
||||
} else {
|
||||
array[mergedArrIndex] = rightArray[rightArrIndex];
|
||||
rightArrIndex++;
|
||||
}
|
||||
mergedArrIndex++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of
|
||||
// left[], if there are any
|
||||
while (leftArrIndex < leftArrLen) {
|
||||
array[mergedArrIndex] = leftArray[leftArrIndex];
|
||||
leftArrIndex++;
|
||||
mergedArrIndex++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of
|
||||
// right[], if there are any
|
||||
while (rightArrIndex < rightArrLen) {
|
||||
array[mergedArrIndex] = rightArray[rightArrIndex];
|
||||
rightArrIndex++;
|
||||
mergedArrIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mergeSort(int arr[], int left, int right) {
|
||||
if (left >= right) {
|
||||
return;
|
||||
}
|
||||
|
||||
int middle = (left + right) / 2;
|
||||
mergeSort(arr, left, middle);
|
||||
mergeSort(arr, middle + 1, right);
|
||||
merge(arr, left, middle, right);
|
||||
}
|
||||
|
||||
// 4 1 3 5 2
|
||||
// 4 1 3 5 2 [4, 1] [3]
|
||||
// 4 1 3 5 2 [^1, 4] [^3]
|
||||
// 1 1 3 5 2 [1, ^4] [^3]
|
||||
// 1 3 3 5 2 [1, ^4] [3]^
|
||||
// 1 3 4 5 2 [^5] [^2]
|
||||
// 1 3 4 2 2 [^5] [2]^
|
||||
// 1 3 4 2 5
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int N = 50;
|
||||
int arr[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
arr[i] = N - i;
|
||||
}
|
||||
|
||||
mergeSort(arr, 0, N - 1);
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
73
cpp/programs/sort_quick.cpp
Normal file
73
cpp/programs/sort_quick.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int partition(int arr[], int start, int end) {
|
||||
int pivot = arr[start];
|
||||
|
||||
int count = 0;
|
||||
for (int i = start + 1; i <= end; i++) {
|
||||
if (arr[i] <= pivot){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
// Giving pivot element its correct position
|
||||
int pivotIndex = start + count;
|
||||
swap(arr[pivotIndex], arr[start]);
|
||||
|
||||
// Sorting left and right parts of the pivot element
|
||||
int i = start, j = end;
|
||||
|
||||
while (i < pivotIndex && j > pivotIndex) {
|
||||
while (arr[i] <= pivot) {
|
||||
// Find index of the first element from left that is higher than pivot
|
||||
i++;
|
||||
}
|
||||
|
||||
while (arr[j] > pivot) {
|
||||
// Find index of the first element from right that is lower than pivot
|
||||
j--;
|
||||
}
|
||||
|
||||
if (i < pivotIndex && j > pivotIndex) {
|
||||
swap(arr[i], arr[j]);
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
return pivotIndex;
|
||||
}
|
||||
|
||||
|
||||
void quickSort(int arr[], int low, int high) {
|
||||
if (low < high) {
|
||||
int pi = partition(arr, low, high);
|
||||
|
||||
quickSort(arr, low, pi - 1);
|
||||
quickSort(arr, pi + 1, high);
|
||||
}
|
||||
}
|
||||
// 4 1 3 5 2
|
||||
// 5 1 3 4 2
|
||||
// 2 1 3 4 5
|
||||
// 1 2 3 4 5
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int N = 50;
|
||||
int arr[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
arr[i] = N - i;
|
||||
}
|
||||
|
||||
quickSort(arr, 0, N - 1);
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user