Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program lab22;
- uses SysUtils;
- Function InputNumber(): Integer;
- const
- MAX_NUMBER = 1000000000;
- MIN_NUMBER = 1;
- var
- IsCorrect: Boolean;
- Number: Integer;
- begin
- Writeln('Введите натуральное число не большее, чем ', MAX_NUMBER);
- repeat
- IsCorrect := true;
- try
- Readln(Number);
- except
- Writeln('Оно должно быть целым числом, которое не больше, чем ',
- MAX_NUMBER);
- IsCorrect := false;
- end;
- if IsCorrect and ((Number < MIN_NUMBER) or (Number > MAX_NUMBER)) then
- begin
- Writeln('Число должно являться положительным и быть не больше, чем ',
- MAX_NUMBER);
- IsCorrect := false;
- end;
- until IsCorrect;
- InputNumber := Number;
- end;
- Function SearchMaxCountOfRepeats(Number: Integer): Integer;
- var
- MaxCountOfRepeats, Counter, Previous, SingleNumber, NewNumber: Integer;
- begin
- MaxCountOfRepeats := 0;
- while (Number > 0) do
- begin
- Counter := 1;
- SingleNumber := Number mod 10;
- Previous := SingleNumber;
- Number := Number div 10;
- NewNumber := Number;
- while (NewNumber > 0) do
- begin;
- SingleNumber := NewNumber mod 10;
- NewNumber := NewNumber div 10;
- begin
- if (Previous = SingleNumber) then
- Inc(Counter);
- if (Counter > MaxCountOfRepeats) then
- MaxCountOfRepeats := Counter;
- end;
- end;
- end;
- SearchMaxCountOfRepeats := MaxCountOfRepeats;
- end;
- Procedure AllSolutions(MaxCountOfRepeats: Integer; Number: Integer);
- var
- Counter, Previous, SingleNumber, NewNumber: Integer;
- begin
- while (Number > 0) do
- begin
- Counter := 1;
- SingleNumber := Number mod 10;
- Previous := SingleNumber;
- Number := Number div 10;
- NewNumber := Number;
- while (NewNumber > 0) do
- begin;
- SingleNumber := NewNumber mod 10;
- NewNumber := NewNumber div 10;
- begin
- if (Previous = SingleNumber) then
- begin;
- Inc(Counter);
- if (Counter = MaxCountOfRepeats) then
- write(Previous, ' ');
- end;
- end;
- end;
- end;
- end;
- Procedure PrintResult(MaxCountOfRepeats: Integer; Number: Integer);
- begin
- Writeln('Самая часто встречающаяся цифра/цифры: ');
- if (MaxCountOfRepeats = 1) then
- Writeln('Такой цифры нет');
- if (MaxCountOfRepeats = 0) then
- Writeln(Number)
- else
- AllSolutions(MaxCountOfRepeats, Number);
- end;
- var
- Number: Integer;
- MaxCountOfRepeats: Integer;
- begin
- Writeln('Программа позволяет узнать, какая цифра в данном числе встречается чаще всего.');
- Number := InputNumber();
- MaxCountOfRepeats := SearchMaxCountOfRepeats(Number);
- PrintResult(MaxCountOfRepeats, Number);
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement