Python:每日一题 174
本帖最后由 冬雪雪冬 于 2018-4-7 20:21 编辑我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:找出斐波那契数列中包含0~9的连续10个数字。
斐波那契数列大家都很熟悉,1, 1, 2, 3, 5, 8, 13, 21, 34, 55......
如果把这些数量组成一个字符串就成为:'11235813213455',这里面'2134'是包含1234这四个数字的,继续下去可以找到第一个含有0~9这10个数字的地方(10个数字的次序随意),可能处于一个斐波那契数,有可能跨越2个或多个斐波那契数,现在要求你找到它,并打印出这十个数字,例如:9034621587 本帖最后由 shigure_takimi 于 2018-4-5 21:05 编辑
def fib(n):
s = ''
a = 0
b = 1
for i in range(n):
a,b = b,a+b
s += str(a)
return s
n = 400
s = fib(n)
length = len(s)
isFind = False
while not isFind:
for i in range(length):
if len(set(s)) == 10:
print(s)
isFind = True
break
else:
n += 200
s = fib(n)
length = len(s)
#第一个数为9034621587。
#如果去掉break,前400个斐波那契数组成的字符串包含如下符合条件的数。
9034621587
5037496128
4538679120
5386791204
0685291437
1307952468
6823514097
0625379814
3495718260
4872635019
8094635172
真的没明白题的意思
本帖最后由 天圆突破 于 2018-4-7 14:54 编辑
def feb():
a = b = 1
while True:
yield str(a)
a,b = b,a+b
def st(string):
n = len(string)
for i in range(n-10):
if len(set(string)) == 10:
return True,string
else:
return False,None
def count():
f, string = feb(), ''
while True:
while len(string) <= 10:
string += next(f)
p = st(string)
if p == True:
print(p)
input()
string = string[-10:]
if __name__ == '__main__':
count()
#!usr/bin/env python
#_*_ coding:utf-8 _*_
def fib(number):
lst = [];
lst.append(1);
lst.append(1);
string = '';
string = string + str(lst);
for i in range(1,number):
lst.append(lst+lst);
string = string + str(lst);
return string;
def test(string):
result = '';
for i in range(len(string)):
temp = string;
if len(temp) == 10:
set1 = set(temp)
if len(set1) == 10:
result = result + temp;
return result;
return result;
if __name__ == '__main__':
number = int(input("请输入你想写多少项:"));
if len(test(fib(number))):
print(test(fib(number)));
else:
print("Nothing");
运行结果:
请输入你想写多少项:200
9034621587
本帖最后由 塔利班 于 2018-4-7 21:27 编辑
a,b,c=0,1,1
s=''
while c:
a,b=b,a+b
s+=str(a)
try:
for i in range(len(s)-10):
if len(set(s))==10:
print(s)
c=0
break
except:
pass
def fib():
a = 0
b = 1
while True:
a, b = b, a+b
yield a
def check_num(ss):
a = []
for each in ss:
if each not in a:
a.append(each)
else:
break
if len(a) == 10:
return True
else:
return False
def main():
ss = ''
num_list = []
NUM = 50
f = fib()
running = True
m = n = 0
while True:
for i in range(NUM):
ss += str(next(f))
m = len(ss)
for i in range(n, len(ss)):
if i <= len(ss)-10:
if check_num(ss):
print('\n找到这个数了:',ss)
print('m=%d, n=%d, NUM=%d' %(m,n,NUM))
running = False
break
else:
break
if running == False:
break
print(ss[(NUM-50):NUM],end='')
n = len(ss) - 10
NUM += 50
if __name__ == '__main__':
main()
def findDigits():
m,n,txt=1,1,"11"#mn分别是数列前后项
while len(set(txt[:10]))!=10:#每次判断前10位,因为10个数字不一样,所以转换成集合后长度为10的符合条件
while len(txt)<11:#文本长度不足时生成数列下一个值
m,n=n,m+n#生成下一个值
txt +=str(n)#新增字符
txt=txt #去掉第一位
return txt[:10]
print(findDigits())
总觉得有点啰嗦,但是又精炼不了 本帖最后由 凌九霄 于 2018-4-5 23:00 编辑
def Fibonacci(n):
fibo =
if n == 1:
return fibo
elif n == 2:
return fibo
else:
for i in range(3, n + 1):
fibo.append(fibo[-2] + fibo[-1])
return fibo
#解决的方法比较笨拙,抛砖引玉看高手们的简便解法
fibstr = ''
for i in Fibonacci(500):
fibstr += str(i)
for j in range(0, len(fibstr)):
numstr = fibstr
if '0' in numstr and '1' in numstr and '2' in numstr and '3' in numstr and '4' in numstr and '5' in numstr and '6' in numstr and '7' in numstr and '8' in numstr and '9' in numstr:
print(numstr)
shu=1000
a=0
b=10
def feibo(shu):
"生成一个斐波那契数列的list并转换为字符串"
a=0
b=1
list_1=[]
for i in range(shu):
a,b=b,a+b
list_1.append(str(a))
list_3 = ''.join(list_1)
return list_3
print(feibo(shu))
while b<len(feibo(shu)):
if '1'in feibo(shu):
if '2' in feibo(shu):
if '3' in feibo(shu):
if '4' in feibo(shu):
if '5' in feibo(shu):
if '6' in feibo(shu):
if '7' in feibo(shu):
if '8' in feibo(shu):
if '9' in feibo(shu):
if '0' in feibo(shu):
print(feibo(shu))
break
a=a+1
b=b+1 import time
def fibo():
a = 1
b = 1
while True:
a, b = b, a+b
yield b
def findallnum(n):
allstr = '11'
fibonum = fibo()
beginindex = 0
count = 0
while True:
for i in range(beginindex, len(allstr) - 9):
checknum = allstr
numberlength = len(set(list(checknum)))
if numberlength == 10:
print(allstr)
count += 1
break
beginindex = len(allstr) - 9
allstr += str(next(fibonum))
if count == n:
break
if __name__ == '__main__':
start = time.time()
findallnum(10)
print(time.time() - start)
1 def fb():
a = 1
b = 1
c = a + b
s = True
str1 = ''
while s:
str1 += str(a)
a = b
b = c
c = a + b
for i in range(len(str1) - 10):
str2 = str1
if fd(str2):
print(str2)
s = False
break
def fd(str2):
list1 = ['0','1','2','3','4','5','6','7','8','9']
for i in str2:
if i in list1:
list1.remove(i)
else:
return False
return True
fb() 本帖最后由 lan358088 于 2018-4-6 10:10 编辑
def fei_bo(n):
a = 0
b = 1
i = 0
fei = []
while i < n:
fei.append(str(b))
a,b = b,a+b
i+=1
qq = ''.join(fei)
return qq
n=10
flag=1
while flag:
qq=fei_bo(n)
num = len(qq)
for h in range(num-10):
aa=list(qq)
bb=aa.copy()
bb.sort()
if bb==['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
print('成功了')
print(aa)
flag=0
break
else:
n+=1
本帖最后由 坑得飞起 于 2018-4-10 00:41 编辑
def findd(ss):
for i in range(10):
if ss.find(str(i))==-1:
return False
print('Answer: ',ss)
return True
def fun(n):
a,b,i,jilu=0,1,0,''
while i<n :
a,b,i,jilu=b,a+b,i+1,jilu[-10:]+str(b)
for j in range(1,len(str(b))+1):
if findd(jilu):
pass
n=int(input())
fun(n)
input() def log(*args, **kwargs):
print(*args, **kwargs)
def fun(number):
if number == 1 or number == 2:
return 1
elif number > 2:
return fun(number-1) + fun(number-2)
else:
return None
list1 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
num = 11
a = 1
while True:
b = 0
sum1 = ''
sum3 = ''
c = False
dict1 = {}
for x in range(a, num):
result = fun(x)
sum1 += str(result)
for lis in list1:
if lis in sum1:
b += 1
if b == 10:
log(sum1)
for sum2 in sum1:
dict1 = 1
for key in dict1.keys():
sum3 += key
log(sum3)
c = True
if c == True:
break
else:
a += 10
num += 10
'''
题目:找出斐波那契数列中包含0~9的连续10个数字。
斐波那契数列大家都很熟悉,1, 1, 2, 3, 5, 8, 13, 21, 34, 55......
如果把这些数量组成一个字符串就成为:'11235813213455',
这里面'2134'是包含1234这四个数字的,
继续下去可以找到第一个含有0~9这10个数字的地方(10个数字的次序随意),
可能处于一个斐波那契数,有可能跨越2个或多个斐波那契数,
现在要求你找到它,并打印出这十个数字,例如:9034621587
'''
#检测给定字符串是否合格
def KO(s):
for i in range(10):
if str(i) not in s:
return False
return True
n = 10
b = True
#为了提高效率,这里不用FB产生数列的项,而是用前面的产生后面的项
a1 = 1
a2 = 1
strfb = "11"
for i in range(1,5):
a1, a2 = a2, a1 + a2
strfb = strfb + str(a2)
print(strfb)
while b:
for i in range(len(strfb)-9):
if KO(strfb):
print(strfb)
print("在", a2, "里找到的,是第", n, "项")
b = False
break
if b:
print(n, "接着找......")
n += 1
a1, a2 = a2, a1 + a2
strfb = strfb[-9:] + str(a2) '''
题目:找出斐波那契数列中包含0~9的连续10个数字。
斐波那契数列大家都很熟悉,1, 1, 2, 3, 5, 8, 13, 21, 34, 55......
如果把这些数量组成一个字符串就成为:'11235813213455',
这里面'2134'是包含1234这四个数字的,
继续下去可以找到第一个含有0~9这10个数字的地方(10个数字的次序随意),
可能处于一个斐波那契数,有可能跨越2个或多个斐波那契数,
现在要求你找到它,并打印出这十个数字,例如:9034621587
'''
#检测给定字符串是否合格
def KO(s):
for i in range(10):
if str(i) not in s:
return False
return True
n = 10
b = True
#为了提高效率,这里不用FB产生数列的项,而是用前面的产生后面的项
a1 = 1
a2 = 1
strfb = "11"
for i in range(1,5):
a1, a2 = a2, a1 + a2
strfb = strfb + str(a2)
print(strfb)
while b:
for i in range(len(strfb)-9):
if KO(strfb):
print(strfb)
print("在", a2, "里找到的,是第", n, "项")
b = False
break
if b:
print(n, "接着找......")
n += 1
a1, a2 = a2, a1 + a2
strfb = strfb[-9:] + str(a2) 啊啊 def fibs():
a, b = 0, 1
while True:
a, b = b, a+b
yield a
string, f = '', fibs()
while True:
if len(set(string)) != 10:
string += str(next(f))
string = string[-11:-1]
else:
print(string)
break
页:
[1]
2