Created
January 12, 2022 12:52
-
-
Save zindmax/6b4f5e08939b7537b409f66fa45a58ea to your computer and use it in GitHub Desktop.
dolgi
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <cmath> | |
#include <algorithm> | |
using namespace std; | |
bool is_valid(vector<int> numb) { | |
int sum = 0; | |
for (int i = 0; i < numb.size(); i++) { | |
sum += pow(2, -numb[i]); | |
} | |
return sum <= 1; | |
} | |
string getBin(int number) { | |
string res = ""; | |
while (x != 0) { | |
if (x % 2 == 0) { | |
res += "0"; | |
}if (x % 2 == 1) { | |
res += "1"; | |
} | |
x /= 2; | |
} | |
return res; | |
} | |
int getDec(string codeWord) { | |
int res = 0; | |
int counter = 0; | |
for (int i = codeWord.size() - 1; i >= 0; i--) { | |
if (codeWord[j] == '1') { | |
res += pow(2, counter); | |
} | |
counter ++; | |
} | |
return res; | |
} | |
int main() | |
{ | |
vector<int> numb = {2,3,3,3,3,4,4,4,4}; | |
vector<string> code; | |
string res = "0"; | |
int x = 0; | |
int counter = 0; | |
if (is_valid(numb)) { | |
for (int i = 0; i < numb.size(); i++) { | |
if (i == 0) { | |
res += string(numb[i] - res.size(), '0'); | |
code.push_back(res); | |
res = ""; | |
}else { | |
x = getDec(code[i-1]); | |
x += 1; | |
res = getBin(x); | |
reverse(res.begin(), res.end()); | |
for (int k = res.size(); k < code[i-1].size(); k++) { | |
res = '0' + res; | |
} | |
for (int t = res.size(); t < numb[i]; t++) { | |
res += '0'; | |
} | |
code.push_back(res); | |
res = ""; | |
} | |
} | |
} | |
for (int i = 0; i < code.size(); i++) { | |
cout << code[i] << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment