#include <iostream> using namespace std; string intToBin(int n) { string result; while (n > 0) { result = (n % 2 == 0 ? '0' : '1') + result; n /= 2; } return result; } int how_many_ones(const string& binary_representation) { int counter = 0; for(char i : binary_representation) if(i == '1') counter++; return counter; } void printGraph(int** graph, unsigned int graph_size) { cout<<graph_size<<endl; for(int i = 1; i <= graph_size; i++) { cout<<graph[i][0]<<" "<<graph[i][1]<<endl; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; string binary_representation = intToBin(n); int how_many_1 = how_many_ones(binary_representation); unsigned int graph_size = how_many_1 + 2*binary_representation.size() - 2; int** graph = new int*[graph_size + 1]; for(int i = 0; i <= graph_size; i++) { graph[i] = new int[2]; } graph[graph_size][0] = -1; graph[graph_size][1] = -1; for(int i = 2; i < 2*binary_representation.size(); i += 2) { graph[graph_size - i][0] = (int)graph_size - i + 2; graph[graph_size - i][1] = (int)graph_size - i + 1; graph[graph_size - i + 1][0] = (int)graph_size - i + 2; graph[graph_size - i + 1][1] = -1; } int iterator = 2*(int)binary_representation.size() - 1; int one_place = 0; while (n > 1) { if(n%2 == 1) { graph[graph_size - iterator][0] = (int)graph_size - iterator + 1; graph[graph_size - iterator][1] = (int)graph_size - one_place; iterator++; } n /= 2; one_place += 2; } // for(int i = binary_representation.size(); i <= graph_size; i++) // { // graph[graph_size - i][0] = (int)graph_size - i + 1; // graph[graph_size - i][1] = // } // printGraph(graph, graph_size); for(int i = 0; i < graph_size; i++) { delete [] graph[i]; } delete [] graph; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <iostream> using namespace std; string intToBin(int n) { string result; while (n > 0) { result = (n % 2 == 0 ? '0' : '1') + result; n /= 2; } return result; } int how_many_ones(const string& binary_representation) { int counter = 0; for(char i : binary_representation) if(i == '1') counter++; return counter; } void printGraph(int** graph, unsigned int graph_size) { cout<<graph_size<<endl; for(int i = 1; i <= graph_size; i++) { cout<<graph[i][0]<<" "<<graph[i][1]<<endl; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; string binary_representation = intToBin(n); int how_many_1 = how_many_ones(binary_representation); unsigned int graph_size = how_many_1 + 2*binary_representation.size() - 2; int** graph = new int*[graph_size + 1]; for(int i = 0; i <= graph_size; i++) { graph[i] = new int[2]; } graph[graph_size][0] = -1; graph[graph_size][1] = -1; for(int i = 2; i < 2*binary_representation.size(); i += 2) { graph[graph_size - i][0] = (int)graph_size - i + 2; graph[graph_size - i][1] = (int)graph_size - i + 1; graph[graph_size - i + 1][0] = (int)graph_size - i + 2; graph[graph_size - i + 1][1] = -1; } int iterator = 2*(int)binary_representation.size() - 1; int one_place = 0; while (n > 1) { if(n%2 == 1) { graph[graph_size - iterator][0] = (int)graph_size - iterator + 1; graph[graph_size - iterator][1] = (int)graph_size - one_place; iterator++; } n /= 2; one_place += 2; } // for(int i = binary_representation.size(); i <= graph_size; i++) // { // graph[graph_size - i][0] = (int)graph_size - i + 1; // graph[graph_size - i][1] = // } // printGraph(graph, graph_size); for(int i = 0; i < graph_size; i++) { delete [] graph[i]; } delete [] graph; return 0; } |