-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
ca5c7e9
commit bfa9fe0
Showing
7 changed files
with
231 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include<stdio.h> | ||
#include<vector> | ||
#include<algorithm> | ||
|
||
int słupki(const std::vector<int> s){ | ||
std::vector<int> kopia; | ||
for(size_t i = 0; i < s.size(); i++){ | ||
kopia.push_back(s[i]); | ||
} | ||
std::sort(kopia.begin(), kopia.end()); | ||
|
||
int mediana = kopia[kopia.size() / 2]; | ||
|
||
int wynik = 0; | ||
|
||
for(size_t i = 0; i < kopia.size(); i++){ | ||
wynik += abs(kopia[i] - mediana); | ||
} | ||
|
||
return wynik; | ||
} | ||
|
||
|
||
int main(){ | ||
printf("%i\n", słupki({6, 4, 3, 4, 5, 2, 3})); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include<stdio.h> | ||
#include<vector> | ||
#include<algorithm> | ||
|
||
bool trojkat(const std::vector<int> v){ | ||
std::vector<int> kopia(v); | ||
std::sort(kopia.begin(), kopia.end()); | ||
|
||
for(size_t i = 2; i < kopia.size(); i++){ | ||
if(kopia[i] <= kopia[i - 1] + kopia[i - 2]) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
int main(){ | ||
printf("%i\n", trojkat({1, 5, 2, 4, 2, 4, 2})); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include<stdio.h> | ||
#include<vector> | ||
#include<algorithm> | ||
#include<queue> | ||
#include<stdlib.h> | ||
|
||
int zaokrDod(int a, int b){ | ||
if(2 * (a % b) >= b) | ||
return a / b + 1; | ||
return a / b; | ||
} | ||
|
||
int zaokrDzielenie(int a, int b){ | ||
return a * b > 0 ? zaokrDod(a, b) : -zaokrDod(abs(a), abs(b)); | ||
} | ||
|
||
int przedzial(const std::vector<int> x, int r){ | ||
std::vector<int> kopia(x); | ||
std::sort(kopia.begin(), kopia.end()); | ||
std::queue<int> q; | ||
|
||
int suma = 0, elementy = 0, maxElementy = 0, wynik = 0; | ||
int c; | ||
bool wszystkieNaleza; | ||
|
||
for(size_t i = 0; i < kopia.size(); i++){ | ||
q.push(kopia[i]); | ||
suma += kopia[i]; | ||
++elementy; | ||
|
||
do | ||
{ | ||
wszystkieNaleza = true; | ||
c = zaokrDzielenie(suma, elementy); //c jest srednia z elementow | ||
if(abs(q.front() - c) > r){ | ||
suma -= q.front(); | ||
--elementy; | ||
q.pop(); | ||
wszystkieNaleza = false; | ||
} | ||
} while (!wszystkieNaleza); | ||
//dla konktertnego i bede usuwal mniejsze elementy az kopia[i] sie zalicza | ||
|
||
if(elementy > maxElementy){ | ||
maxElementy = elementy; | ||
wynik = c; | ||
} | ||
} | ||
|
||
return wynik; | ||
} | ||
|
||
int main(){ | ||
printf("%i\n", przedzial({2, -2, 5, -1, 11, 8, 4, 5, 8, 7}, 2)); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include<stdio.h> | ||
#include<vector> | ||
#include<algorithm> | ||
|
||
std::vector<float> przekładaniec(const std::vector<float> x){ | ||
std::vector<float> v(x); | ||
std::sort(v.begin(), v.end()); | ||
std::vector<float> wynik; | ||
|
||
size_t odwiedzone = 1; | ||
size_t i = 0, j = v.size() - 1; | ||
wynik.push_back(v[(v.size() - 1) / 2]); | ||
|
||
while (odwiedzone < v.size() - 1) | ||
{ | ||
wynik.push_back(v[j--]); | ||
wynik.push_back(v[i++]); | ||
|
||
odwiedzone += 2; | ||
} | ||
|
||
if(odwiedzone < v.size()) | ||
wynik.push_back(v[(v.size() - 1) / 2 + 1]); | ||
|
||
return wynik; | ||
} | ||
|
||
int main(){ | ||
std::vector<float> wynik = przekładaniec({1, 2, 3, 4, 6}); | ||
for(auto val : wynik) | ||
printf("%lf ", val); | ||
|
||
printf("\n"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include<vector> | ||
#include<queue> | ||
#include<stdio.h> | ||
|
||
void przelej(std::queue<float> &v, std::queue<float> &q){ | ||
float zlane = 0; | ||
for(int i = 0; i < 2; i++){ | ||
if(q.empty() || v.front() < q.front()){ | ||
zlane += v.front(); | ||
v.pop(); | ||
} else { | ||
zlane += q.front(); | ||
q.pop(); | ||
} | ||
} | ||
|
||
q.push(zlane); | ||
} | ||
|
||
float zlewki(const std::vector<float> x, int k){ | ||
std::queue<float> v, q; | ||
for(float val : x) v.push(val); | ||
|
||
for(int i = 0; i < k; ++i){ | ||
przelej(v, q); | ||
} | ||
|
||
if(!v.empty() && !q.empty()) | ||
return std::min(v.front(), q.front()); | ||
else if(v.empty()) | ||
return q.front(); | ||
else | ||
return v.front(); | ||
|
||
} | ||
|
||
int main(){ | ||
printf("%lf\n", zlewki({1, 2, 3, 4, 5}, 3)); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
typedef struct node *bin_tree; | ||
struct node { | ||
int val; | ||
bin_tree left, right; | ||
}; | ||
|
||
bin_tree newNode(int val){ | ||
bin_tree nowy = (node*)malloc(sizeof(node)); | ||
nowy->val = val; | ||
nowy->left = nowy->right = NULL; | ||
return nowy; | ||
} | ||
|
||
void del_subtree(bin_tree t){ | ||
if(t == NULL) return; | ||
del_subtree(t->left); | ||
del_subtree(t->right); | ||
free(t); | ||
} | ||
|
||
void bestify_subtree(bin_tree t, int maxx, int minn, bin_tree *pToME){ | ||
if(!t) return; | ||
if(t->val >= maxx || t->val <= minn){ | ||
del_subtree(t); | ||
*pToME = NULL; | ||
return; | ||
} | ||
|
||
bestify_subtree(t->left, t->val, minn, &(t->left)); | ||
bestify_subtree(t->right, maxx, t->val, &(t->right)); | ||
} | ||
|
||
|
||
int main(){ | ||
bin_tree t = newNode(10); | ||
t->left = newNode(5); | ||
t->left->right = newNode(15); | ||
t->right = newNode(14); | ||
|
||
bestify_subtree(t, __INT16_MAX__, -__INT_MAX__, &t); | ||
return 0; | ||
} |