
ПОМОГИТЕ!!! ОЧЕНЬ НАДО!!!Дано многоразрядное число N<=10^9. Найти среднее арифметическое цифр
числа, разряды которых равны 2 и 5. P.S. Я уже задавала эту задачу, но у меня ее не приняли так как нужно через цикл

Ответы на вопрос

var
n:longint;
a,b,k,d:integer;
begin
Write('Введите натуральное число длиной 6-9 цифр: ');
Read(n);
k:=0;
while n>0 do begin
d:=n mod 10;
if k=2 then a:=d
else
if k=5 then b:=d;
Inc(k);
n:=n div 10
end;
Writeln('Ср. арифм. разрядов 2 и 5 равно ',(a+b)/2)
end.
Пример
Введите натуральное число длиной 6-9 цифр: 123456789
Ср. арифм. разрядов 2 и 5 равно 5.5



Problem Analysis
To find the arithmetic mean of the digits in a given multi-digit number, we need to: 1. Extract the individual digits from the number. 2. Calculate the sum of the digits that are equal to 2 or 5. 3. Count the number of digits that are equal to 2 or 5. 4. Divide the sum by the count to find the arithmetic mean.Solution
Here's a step-by-step solution to find the arithmetic mean of the digits in a given multi-digit number, where the digits are equal to 2 or 5:1. Convert the given number to a string to easily extract the individual digits. 2. Initialize two variables: `sum` to keep track of the sum of the digits and `count` to keep track of the number of digits that are equal to 2 or 5. 3. Iterate through each character in the string representation of the number. 4. Check if the character is equal to '2' or '5'. If it is, convert it back to an integer and add it to the `sum`. Also, increment the `count` by 1. 5. After the iteration, check if `count` is greater than 0 to avoid division by zero. 6. If `count` is greater than 0, calculate the arithmetic mean by dividing `sum` by `count`. 7. Return the arithmetic mean as the result.
Here's the code implementation in Python:
```python def find_arithmetic_mean(number): number_str = str(number) sum = 0 count = 0
for digit in number_str: if digit == '2' or digit == '5': sum += int(digit) count += 1
if count > 0: arithmetic_mean = sum / count return arithmetic_mean else: return 0 ```
Example Usage
Let's say we have the number 25345. We can use the `find_arithmetic_mean` function to find the arithmetic mean of the digits that are equal to 2 or 5:```python number = 25345 arithmetic_mean = find_arithmetic_mean(number) print(arithmetic_mean) ```
The output will be: ``` 3.5 ```
In this example, the arithmetic mean of the digits 2 and 5 in the number 25345 is 3.5.
I hope this helps! Let me know if you have any further questions.


Топ вопросов за вчера в категории Информатика








Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili