快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

python函数求同构数 同构数python11000

怎么用python判断一个数是否是同构数?

利用数据类型转换讲数字问题变成字符串比对。将数字平方让后转换成字符串然后利用索引做切片看与数字本身转换成字符串的量是否相等。这个方法充分利用了python的内置丰富的内置函数。

成都创新互联网站建设提供从项目策划、软件开发,软件安全维护、网站优化(SEO)、网站分析、效果评估等整套的建站服务,主营业务为成都网站建设、做网站,成都app开发以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。成都创新互联深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

随机输入若干个不超过2位的正整数(输入-1表示输入结束),找出其中所有同构数并排序输出。(正整数n若是它平方数的尾部,则称n为同构数。如5的平方数是25,且5是25的尾部,那么5就是一个同构数。同理,25的平方为625,25也是同构数)。

简介

输入时要求首先判断输入数位数(1-2位)是否正确,判断输入内容是否是数字,提示:判断一个字符串是否是数字字符可用字符串的isdigit方法然后判断是否是同构数。

输出的同构数要求从小到大排序,结果显示在一行,各同构数间空格分隔。提示:排序可用列表的sort方法。

python中求同构数

for index in range(1,10000):

tmp =index *index

tmp1 =1

for helpIndex in range(1,10):

tmp1 = tmp1*10

if index%tmp1 ==index:

break

if tmp%tmp1 == index:

print ("this is the need value " + str(index) +" and the ^2 is " + str(tmp))

下面是结果的数据:

this is the need value 1 and the ^2 is 1

this is the need value 5 and the ^2 is 25

this is the need value 6 and the ^2 is 36

this is the need value 25 and the ^2 is 625

this is the need value 76 and the ^2 is 5776

this is the need value 376 and the ^2 is 141376

this is the need value 625 and the ^2 is 390625

this is the need value 9376 and the ^2 is 87909376

如何用Python切片求出1000以内的同构数?

for i in range(1,1001):

if str(pow(i,2))[len(str(i))*(-1):]==str(i):

  print(i,end="/")

结果是:1/5/6/25/76/376/625/

思路如下:

利用range函数遍历1到1000的整数

充分利用了Python自身的特性,将数字问题转换成字符串比较利用数字类型的转换.

转换成字符串就可以利用切片了.不说太详细了自己思考一下吧


网站标题:python函数求同构数 同构数python11000
本文URL:http://6mz.cn/article/dopoods.html

其他资讯