/
Oppq
/
Labs
Обзор
Документация
Войти
/
Oppq
/
Labs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lab7/Part1/12.cpp
414 строк
7 KB
Oppq
final upd
15 апр 2026, 18:19
Верифицирован
15 апр 2026, 18:19
e0a6978
Код
Авторство
О чём код?
//Задание 1 //1.1 #include <iostream> using namespace std; int main() { int n; std::cin >> n; if (n >= 32) { return 1; } // Вычисляем 2^n с помощью побитового сдвига unsigned int result = 1 << n; std::cout << result; return 0; } //1.2 #include <iostream> using namespace std; int main() { unsigned int n, m; cin >> n >> m; unsigned int result = (1 << n) | (1 << m); cout << result << endl; return 0; } //Задание 2 //2.1 #include <iostream> using namespace std; int main() { unsigned int A, i; cin >> A >> i; unsigned int result = A | (1 << i); cout << result << endl; return 0; } //2.2 #include <iostream> using namespace std; int main() { int A, i; cin >> A >> i; int result = A & ~(1 << i); cout << result << endl; return 0; } //Задание 3 #include <iostream> using namespace std; int main() { long long A, i; cin >> A >> i; int result; if ((A | (1 << i)) == A) {result = 1;} else {result = 0;} cout << result; return 0; } //Задание 4 #include <iostream> using namespace std; int main() { int m, n; cin >> m >> n; while (n != 0) { int temp = n; n = m % n; m = temp; } cout << m << endl; return 0; } //Задание 5 Пока только основная логика - Каждая 16-ричная цифра = ровно 4 бита в двоичной системе. //5.1 #include <iostream> #include <string> #include <map> using namespace std; int main() { string h, s = ""; cin >> h; // 0123456789101112131415 // 0123456789A B C D E F map<char, string> perevod = { {'0',"0000"}, {'1',"0001"}, {'2',"0010"}, {'3',"0011"}, {'4',"0100"}, {'5',"0101"}, {'6',"0110"}, {'7',"0111"}, {'8',"1000"}, {'9',"1001"}, {'A',"1010"}, {'B',"1011"}, {'C',"1100"}, {'D',"1101"}, {'E',"1110"}, {'F',"1111"} }; for (char c : h) { s = s + perevod[c]; } while (s.length() > 0 && s[0] == '0') { s.erase(0, 1); } if (s == "") { cout << "0" << endl; } else { cout << s << endl; } return 0; } //5.2 #include <iostream> #include <string> #include <map> using namespace std; int main() { string b, s = ""; cin >> b; map<string, char> perevod = { {"0000",'0'}, {"0001",'1'}, {"0010",'2'}, {"0011",'3'}, {"0100",'4'}, {"0101",'5'}, {"0110",'6'}, {"0111",'7'}, {"1000",'8'}, {"1001",'9'}, {"1010",'A'}, {"1011",'B'}, {"1100",'C'}, {"1101",'D'}, {"1110",'E'}, {"1111",'F'} }; // Дополняем нулями слева до кратности 4, поскольку 2^4 = 16 while (b.length() % 4 != 0) { b = "0" + b; } for (int i = 0; i < b.length(); i = i + 4) { string fB = b.substr(i, 4); s = s + perevod[fB]; } while (s.length() > 0 && s[0] == '0') { s.erase(0, 1); } if (s == "") { cout << "0" << endl; } else { cout << s << endl; } return 0; } //Задание 6 #include <iostream> using namespace std; int main() { unsigned int A; // без int работает также, с char ошибка. Разобрать cin >> A; for (int i = 7; i >= 0; i--) { if ((A >> i) & 1) {cout << '1';} else {cout << '0';} } cout << endl; return 0; } //Задание 7 #include <iostream> #include <cmath> #include <string> using namespace std; int main() { int N; cin >> N; int total = pow(2, N); for (int i = 0; i < total; i++) { string s = ""; int temp = i; for (int i = 0; i < N; i++) { if (temp % 2 == 0) {s = "0" + s;} else {s = "1" + s;} temp = temp / 2; } cout << s << endl; } return 0; } //Задание 8 #include <cassert> #include <iostream> class Fibonacci { public: static int get(int n) { assert(n >= 0); if (n == 0) return 0; if (n == 1) return 1; int prev = 0; // Fn-2 int curr = 1; // Fn-1 for (int i = 2; i <= n; i++) { int next = prev + curr; prev = curr; curr = next; } return curr; } }; int main(void) { int n; std::cin >> n; std::cout << Fibonacci::get(n) << std::endl; return 0; } //Задание 9 (без начального кода) #include <iostream> using namespace std; int main() { int n; cin >> n; // Период последних цифр = 60 n = n % 60; int prev = 0; // F0 int curr = 1; // F1 if (n == 0) { cout << 0; return 0; } for (int i = 2; i <= n; i++) { int next = (prev + curr) % 10; // только последняя цифра prev = curr; curr = next; } cout << curr; return 0; } //Задание 9 с начальным заданным кодом #include <cassert> #include <iostream> class Fibonacci { public: static int get_last_digit(int n) { assert(n >= 1); int a = 0, b = 1, temp; for (int i = 1; i < n; i++) { temp = (a + b) % 10; a = b; b = temp; } return b; } }; int main(void) { int n; std::cin >> n; std::cout << Fibonacci::get_last_digit(n) << std::endl; return 0; } //Задание 10 #include <iostream> #include <queue> #include <string> using namespace std; int main() { int n; cin >> n; priority_queue<int> pq; // max-heap по умолчанию for (int i = 0; i < n; i++) { string operation; cin >> operation; if (operation == "Insert") { int x; cin >> x; pq.push(x); } else if (operation == "ExtractMax") { cout << pq.top() << endl; pq.pop(); } } return 0; }