全部评论
有这个题目?
算法第一题吗
求题目
超时,哭了
求思路
全排列 百分之10 0.0
# -*-coding:utf-8 -*-
import sys
import numpy as np
from collections import OrderedDict
import collections
def print_lists(lists):
"""
打印二维列表
:param lists: List[List[int]]
:return: None
"""
for list in lists:
for i, x in enumerate(list):
print(x, end=' ')
print(end='\n')
# def fib(n, a, x):
# list = list()
# list.append(n)
# list.append(a)
# b = n + a
# list.append(b)
# while b <= max:
# b = n + a
# list.append(b)
# n, a = a, b
#
# return list
def fib(n , a, max):
list = []
b = 0
list.append(n)
list.append(a)
while b <= max:
b = n + a
n, a = a, b
list.append(b)
# a, b = b, a + b
# list.append(b)
# if max in list:
# weizhi = list.index(max)
return list
def solution(x):
ret = []
array = []
if x < 3:
return
if x == 3:
list1 = fib(1, 1, x)
list2 = fib(1, 2, x)
list3 = fib(2, 1, x)
array.append(list1)
array.append(list2)
array.append(list3)
else:
list1 = fib(1, 1, x)
list2 = fib(1, 2, x)
list3 = fib(2, 1, x)
array.append(list1)
array.append(list2)
array.append(list3)
for i in range(4, x+1, 1):
if i%2 == 0:
list1 = []
a = int(i/2)
list1 = fib(a, a, x)
array.append(list1)
else:
n = int(i/2)
a = int(i/2)+1
list1 = fib(n, a, x)
array.append(list1)
list1 = fib(a, n, x)
array.append(list1)
hash = OrderedDict()
print_lists(array)
for list in array:
if x in list:
weizhi = list.index(x) + 1
hash[weizhi] = hash.get(weizhi, 0) + 1
hash = OrderedDict(sorted(hash.items(), key=lambda x: x[0], reverse=False))
return hash
if __name__ == "__main__":
# 读取第一行的n
ret = []
for i in range(1):
x = int(sys.stdin.readline().strip())
# print(x)
ret_hash = solution(x)
for i, j in ret_hash.items():
ret.append([i, j])
print_lists(ret) 你看看我这个答案可以没,我自己测试是没问题,不过提示数组越界了。
相关推荐
点赞 评论 收藏
分享
//鲨鱼辣椒:什么什么都作废了,如同一场大雪覆盖了前半生的荒唐
点赞 评论 收藏
分享