Python计算回文数的方法
作者:saintatgod 时间:2022-04-02 05:08:07
本文实例讲述了Python计算回文数的方法。分享给大家供大家参考。具体如下:
这里检查数字是不是回文数,用196算法生成一个数字的回文数
num = 905;
def is_Palindrome(num):
"""
判断一个数字是不是回文数,这里有些取巧了
:param num:
:return:
"""
"""
:param num:
:return:
"""
temp = "%d"%num;
str = temp[::-1];
if temp == str:
return True;
else:
return False;
def create_Palindrome(num):
"""
用196算法计算指定数字的回文数
:param num:
:return:
"""
count = 0;
while True:
if True == is_Palindrome(num):
output = "这是一个回文数:%d"%num + "\r\n总共次数为%d"%count;
print(output);
break;
else:
num = add(num);
count += 1;
def add(num):
"""
num 与自己倒序的数字相加
:param num:
:return:
"""
temp = "%d"%num;
str = temp[::-1];
return int(temp) + int(str);
print(create_Palindrome(num));
希望本文所述对大家的Python程序设计有所帮助。
标签:Python,计算,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
文档标准的真实谎言
2008-06-02 10:46:00
用Dreamweaver MX制作文字特效
2011-06-14 09:49:47
![](https://img.aspxhome.com/file/UploadPic/20072/20072311216835s.gif)
Django的get_absolute_url方法的使用
2023-05-28 02:38:14
![](https://img.aspxhome.com/file/2023/9/92369_0s.png)
Python列表1~n输出步长为3的分组实例
2021-10-31 04:25:27
python Kmeans算法原理深入解析
2022-03-01 07:26:55
![](https://img.aspxhome.com/file/2023/8/95548_0s.png)
使用Python获取字典键对应值的两种方法
2022-08-04 05:25:16
![](https://img.aspxhome.com/file/2023/7/117267_0s.png)
python实现ip代理池功能示例
2023-07-30 16:36:06
Python序列化模块JSON与Pickle
2022-11-06 00:24:37
浅谈python量化 双均线策略(金叉死叉)
2022-05-28 02:21:58
![](https://img.aspxhome.com/file/2023/0/130670_0s.jpg)
Linux下升级python和安装pip的详解
2022-08-23 06:27:15
![](https://img.aspxhome.com/file/2023/4/110154_0s.png)
torch.optim优化算法理解之optim.Adam()解读
2023-06-24 23:30:25
![](https://img.aspxhome.com/file/2023/9/72739_0s.png)
Pytorch深度学习经典卷积神经网络resnet模块训练
2022-12-02 01:43:23
![](https://img.aspxhome.com/file/2023/4/120304_0s.png)
python中split(), os.path.split()和os.path.splitext()的用法
2022-03-23 09:06:40
![](https://img.aspxhome.com/file/2023/1/123821_0s.png)
python3 实现自定义切片类为左闭右闭详情
2022-11-02 11:36:51
使用Python的Flask框架实现视频的流媒体传输
2023-05-07 02:46:53
![](https://img.aspxhome.com/file/2023/2/69512_0s.jpg)
Python 中将值附加到集合的操作方法
2022-01-06 15:09:54
appium测试之APP元素定位及基本工具介绍
2021-09-24 20:51:47
![](https://img.aspxhome.com/file/2023/6/104986_0s.png)
python分析inkscape路径数据方案简单介绍
2021-05-13 14:51:09
![](https://img.aspxhome.com/file/2023/6/127326_0s.png)
linux 下python多线程递归复制文件夹及文件夹中的文件
2021-06-28 03:27:44
详解SQL Server中数据库快照工作原理
2009-01-21 14:18:00