#include<iostream> using namespace std; int suma_binarna(int dl, int liczba){ int wynik = 0; int potega = 2; for(int i = 1; i < dl; i++){ potega *= 2; } for(int i = dl + 1; i > 0; i--){ if(potega <= liczba){ liczba -= potega; wynik++; } if(0 == liczba){ break; } potega /= 2; } return wynik; } int main(){ int n; cin >> n; //cout << suma_binarna(3, n); int *odp = new int[n]; int dl = 1; int j = 0; while(n > 0){ j++; int suma = suma_binarna(dl, j); if(dl == suma){ dl++; } odp[j-1] = j; n -= suma; if(suma == dl - 1 and n < 0){ dl--; } } int dl_ciagu = j; int ost_element = j - 1; while(n < 0){ j--; int suma = suma_binarna(dl, j); if(-1*n >= suma){ n += suma; odp[j-1] = 0; dl_ciagu--; } } cout << dl_ciagu << "\n"; for(int i = ost_element; i > -1 ; i--){ if(odp[i] != 0){ cout << odp[i] << " "; } } delete [] odp; 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 | #include<iostream> using namespace std; int suma_binarna(int dl, int liczba){ int wynik = 0; int potega = 2; for(int i = 1; i < dl; i++){ potega *= 2; } for(int i = dl + 1; i > 0; i--){ if(potega <= liczba){ liczba -= potega; wynik++; } if(0 == liczba){ break; } potega /= 2; } return wynik; } int main(){ int n; cin >> n; //cout << suma_binarna(3, n); int *odp = new int[n]; int dl = 1; int j = 0; while(n > 0){ j++; int suma = suma_binarna(dl, j); if(dl == suma){ dl++; } odp[j-1] = j; n -= suma; if(suma == dl - 1 and n < 0){ dl--; } } int dl_ciagu = j; int ost_element = j - 1; while(n < 0){ j--; int suma = suma_binarna(dl, j); if(-1*n >= suma){ n += suma; odp[j-1] = 0; dl_ciagu--; } } cout << dl_ciagu << "\n"; for(int i = ost_element; i > -1 ; i--){ if(odp[i] != 0){ cout << odp[i] << " "; } } delete [] odp; return 0; } |