gpt4 book ai didi

python 吸血鬼号

转载 作者:太空狗 更新时间:2023-10-30 02:56:45 25 4
gpt4 key购买 nike

<分区>

第一次在这个社区发帖。我最近开始学习 Python(2 周),作为练习的一种方式,一位同学给了我一个任务:“编写一个程序来检查整数是否是吸血鬼数。要使整数成为吸血鬼数字 (v),它需要满足以下 4 个条件:

1)有一对数字。让我们调用位数:n

2)你可以通过将两个整数x和y相乘得到v,每个整数都有n/2位。 x 和 y 是毒牙。

3)两个尖牙不能同时以0结尾。

4)v 可以由 x 和 y 中的所有数字组成,顺序任意,每个数字只使用一次。

例子:

21*60=1260 <--吸血鬼数量

210*600=126000 <--不是吸血鬼号

The first vampire numbers are the following: 1260, 1395, 1435, 1530, 1827, 2187, 6880, 102510, 104260, 105210, 105264, 105750, 108135, 110758, 115672, 116725, 117067, 118440, 120600, 123354 , 124483, 125248, 125433, 125460, 125500, 126027, 126846, 129640, ...

到目前为止,我已经制作了一个可以达到前 3 个标准的程序(我认为)。我正在寻求有关最后一个问题的帮助。

这就是我得到的:(抱歉有些东西是西类牙语)

v=int(input("Enter number to test for vampire:"))
#Test for pair number of digits
def nd(a):
nd = 0
while a != 0:
d = a % 10
if d != 0:
nd += 1
a = a // 10
return nd
def DigitosPar(a):
if nd(a)%2==0:
return 1
else:
return 0
#Last digit is 0
def UltimoDigCero(b):
ud = 0
ud = b % 10
if ud==0:
return 1
else:
return 0

if DigitosPar(v)==1:
x=[]
for i in range(int(10**(nd(v)/2-1)),int(10**(int(nd(v))/2))):
x.append(i)
y=x
z=0
posiblex=0
posibley=0
for ia in range(0,len(y)):
for ib in range(0,len(x)):
z=y[ia]*x[ib]
if z==v and not((UltimoDigCero(x[ib])==1 and UltimoDigCero(y[ia])==1)):
posiblex=x[ib]
posibley=y[ia]
print(v,"has as fangs",posiblex,posibley)
if posiblex==0:
print(v, "not a vampire")
else:
print(v, "not a vampire")

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com