/
AirLexa
/
03
Обзор
Документация
Войти
/
AirLexa
/
03
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lesson_06/Task_2/Task_2.cpp
41 строка
1 KB
AirLexa
загрузка Lesson_06/Task_2/Task_2.cpp
20 янв 2026, 22:31
Верифицирован
20 янв 2026, 22:31
93f1ae4
Код
Авторство
О чём код?
#include <iostream> #include <string> #include <windows.h> int real_string_hash(std::string &s, int p, int n) { unsigned long long tmp = 0; for (int i = 0; i < s.length(); i++) { unsigned long long tmp_2 = s[i] * std::pow(p, i); if (ULLONG_MAX - tmp_2 < tmp) return -1; // при сложении будет переполнение tmp += tmp_2; } return static_cast<int>(tmp % n); } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int p = 0, n = 0; std::cout << "Введите p: "; std::cin >> p; std::cout << "Введите n: "; std::cin >> n; std::cin.clear(); std::cin.ignore(INT_MAX, '\n'); std::string word = ""; while (word != "exit") { std::cout << "Введите строку: "; getline(std::cin, word); int hash = real_string_hash(word, p, n); if (hash < 0) std::cout << "Ошибка переполнения для \"" << word << "\""; else std::cout << "Хэш строки \"" << word << "\" = " << hash; std::cout << std::endl; } std::cout << "До свидания! До новых встреч!" << std::endl; return 0; }