Skip to content

Commit

Permalink
Add zagadka o coli
Browse files Browse the repository at this point in the history
  • Loading branch information
stopnoanime committed Nov 15, 2023
1 parent 89e8195 commit f75e246
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ _WDP\* MIM UW_

- [zad. 3.]
- [zad_3](./src/cw6/zad3/zad3_sadzawka.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 5.]
- [katastrofy](./src/cw6/zad5/katastrofy.cpp) (C++) :white_check_mark::microscope:
- [zad. 7.]
- [zad_7](./src/cw6/zad7/zad7_skyline.cpp): autor: @pixelkubek (C++) :white_check_mark:
- [zad. 9.]
Expand Down Expand Up @@ -172,6 +174,7 @@ _WDP\* MIM UW_

- [fibonacci_matrix](./src/other/fibonacci_matrix.c)
- [fibonacci](./src/other/fibonacci.c)
- [cola 42](./src/other/cola.cpp)

## Legenda

Expand Down
69 changes: 69 additions & 0 deletions src/other/cola.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <bitset>
#include <iostream>
#include <string>

/*
0
01
0110
01101001
01101001
0110
01
0
*/

using namespace std;

bool sprawdzRozdzial(string s, string &wynik) {
for (int i = 0; i < s.size(); i += 2) {
if (i < s.size() - 1) {
if (s[i] == '0' && s[i + 1] == '1') {
wynik += "0";
} else if (s[i] == '1' && s[i + 1] == '0') {
wynik += "1";
} else {
// nie da sie
return false;
}
} else { // ostatni element
wynik += s[i];
}
}

return true;
}

bool czyWCiagu(string s) {
string wynik;

if (s.size() <= 3)
return !(s == "111" || s == "000");

if (sprawdzRozdzial(s, wynik))
return czyWCiagu(wynik);

// Probujemy offset +1
wynik = (s[0] == '1') ? "0" : "1";
s.erase(0, 1);

if (sprawdzRozdzial(s, wynik))
return czyWCiagu(wynik);

return false;
}

int main() {
int ile = 0;
const int potega = 14;
const int warPotega = 1 << potega;

for (int i = 0; i < warPotega; i++) {
string s = bitset<(size_t)potega>(i).to_string();
if (czyWCiagu(s))
ile++;
}

cout << ile << " " << (float)ile / (float)warPotega;
}

0 comments on commit f75e246

Please sign in to comment.