Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int inputNumber() {
- const int MAX_NUMBER = 100000000;
- bool isIncorrect;
- int number;
- string input = "";
- printf("Введите натуральное число не большее, чем %d \n", MAX_NUMBER);
- do {
- isIncorrect = false;
- getline(cin, input);
- try {
- number = stoi(input);
- }
- catch (invalid_argument ex) {
- cout << "Нужно ввести число.\n";
- isIncorrect = true;
- }
- catch (out_of_range ex) {
- printf("Нужно ввести число, которое не больше, чем %d\n", MAX_NUMBER);
- isIncorrect = true;
- }
- if (!isIncorrect && (number < 1 || number > MAX_NUMBER)) {
- cout << "Число должно являться положительным и быть не больше " << MAX_NUMBER << "\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return number;
- }
- int searchMaxCountOfRepeats(int number) {
- int maxCountOfRepeats, counter, previous, singleNumber, newNumber;
- maxCountOfRepeats = 0;
- while (number > 0) {
- counter = 1;
- singleNumber = number % 10;
- previous = singleNumber;
- number = number / 10;
- newNumber = number;
- while (newNumber > 0)
- {
- singleNumber = newNumber % 10;
- newNumber = newNumber / 10;
- if (previous == singleNumber)
- counter++;
- if (counter > maxCountOfRepeats)
- maxCountOfRepeats = counter;
- }
- }
- return maxCountOfRepeats;
- }
- void allSolutions(int number, int maxCountOfRepeats) {
- int counter, previous, singleNumber, newNumber;
- while (number > 0) {
- counter = 1;
- singleNumber = number % 10;
- previous = singleNumber;
- number = number / 10;
- newNumber = number;
- while (newNumber > 0)
- {
- singleNumber = newNumber % 10;
- newNumber = newNumber / 10;
- if (previous == singleNumber)
- {
- counter++;
- if (counter == maxCountOfRepeats)
- cout << previous << "; ";
- }
- }
- }
- }
- void printResult(int number, int maxCountOfRepeats)
- {
- cout << "Самая часто встречающаяся цифра/цифры: ";
- if (maxCountOfRepeats == 1)
- cout << "Такой цифры нет";
- if (maxCountOfRepeats == 0)
- cout << number;
- else
- allSolutions(number, maxCountOfRepeats);
- }
- int main() {
- setlocale(LC_ALL, "Russian");
- int maxCountOfRepeats, number;
- cout << "Программа позволяет узнать, какая цифра в данном числе встречается чаще всего.\n";
- number = inputNumber();
- maxCountOfRepeats = searchMaxCountOfRepeats(number);
- printResult(number, maxCountOfRepeats);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement