Предмет: Информатика, автор: Milany2548

ПРОГРАММА НА ПИТОН!!!!
Палиндром - это число, одинаково читающееся в обоих направлениях (не меняется при перестановке своих цифр в обратном порядке).
Дано натуральное число K. Выведите на экран количество натуральных палиндромов, не превосходящих число К.

Входные данные
Задано единственное число K (1<=K<=100000).

Выходные данные
Необходимо вывести количество натуральных палиндромов, не превосходящих K.

Примеры
Входные данные -
1
1
Выходные данные -
100
18
Запрещенные операторы: list;[;find;index;reverse

Ответы

Автор ответа: mathgenius
0

Первый способ.

def ispalindrom(n): #Если палиндром True, иначе False.

   k = n

   m = 0

   while k!= 0:

       m = 10 * m + k % 10

       k //= 10

   return m == n

print(f'Количество палиндромов:',sum(map(ispalindrom, range(1, int(input('Введите число K: '))+1))))

Второй способ. (c использованием строк)

print(f'Количество палиндромов:',sum(map(lambda x: str(x) == str(x)[::-1], range(1, int(input('Введите число K: '))+1))))

Приложения:

mathgenius: Можно решить быстрее через строки, но не знаю, можно ли их использовать в
mathgenius: этой задаче
mathgenius: А вообще задачка легко считается математически, можно вывести формулу количества палиндромов на интервале, но в решении этого делать не стал, ибо это не цель задания...
mathgenius: Через строки проверка на палиндром может быть записана в одну строчку: n == n[::-1] , тут n это строковый номер.
mathgenius: В этом случае решение уложится в одну строчку:
mathgenius: print(f'Количество палиндромов:',sum(map(lambda x: str(x) == str(x)[::-1], range(1, int(input('Введите число K: '))+1))))
Похожие вопросы
Предмет: Английский язык, автор: rerke98
Переведите текст, пожалуйста( только правильно! )
Laura was eating her bread and butter as she looked at the workmen walking to the place where the tent would be put up. Only the tall man was behind. He bent down, picked a flower, put it to his nose, and smelt it. When Laura saw that she forgot about the tent. She wondered at him- he cared for the smell of a flower. Oh, how wonderfully nice workmen for friends rather than the silly boys with whom she danced? It is all the fault of these absurd class distinctions.
"Laura, Laura, where are you? Telephone, Laura!" a voice cried from the house.
"Coming!" And she ran over the lawn, across the veranda and into the house. In the hall her father and her brother Laurie were putting on their hats ready to go to the office.
After a talk over the telephone with her friend Laura put back the receiver and sat still listening. All the doors in the house seemed to be open.
The front door- bell rang and their maid, Sadie, ran to open the door. A man said something. Sadie answered “Oh! I don’t know. Wait, I’ll ask Mrs. Sheridan.”
"What is it, Sadie?" Laura came into the hall.
"It's the florist, Miss Laura."
Near the door stood a wide tray full of pots of red lilies. Nothing but lilies, big red flowers.
"O¬-oh, Sadie!" said Laura. She bent down to the mass of lilies. "It's some mistake," she said. "Nobody ever ordered so many. Sadie, go and find mother."
But at that moment Mrs. Sheridan joined them.
"It's quite right," she said. "Yes, I ordered them. Aren't they beautiful?" She touched Laura's arm. "I was passing the shop yesterday, and I saw them in the window. And I suddenly thought for once in my life I shall have enough lilies. The garden ¬party will be a good excuse."
"But I thought you said you didn't want to take part in the preparations", said Laura. She put her arm round her mother's neck and kissed her.
"My dear child, you wouldn't like a logical mother, would you?”
In the kitchen the cook had finished making the sandwiches.
"Godber's has come", said the maid, coming, into the kitchen.. She had seen the man pass the window.
That meant the cream puffs had come. Godber's were famous for their cream puffs. Nobody ever thought of making them at home.
"Bring them in and put them on the table, my girl", ordered cook. Sadie brought them in and went back to the door. Laura and Jose thought they were too grown¬up to care about such things. All the same they agreed that the puffs looked very good. Very.
"Have one each, my dears," said cook. "Your mother won't know." Oh, impossible. Cream puffs so soon after breakfast. But two minutes later Jose and Laura were eating them with great pleasure.
"Let's go into the garden by the backdoor", suggested Laura. "I want to see the men putting up the tent.. They're such nice men."
But at the back door stood cook, Sadie, Godber's man and Hans, the boy who worked in the house. Something had happened.
Предмет: Математика, автор: borisborisvasilev198