【发布时间】:2015-06-28 09:30:28
【问题描述】:
问题: M 个算术级数,每个具有 N 项(项相差 d1,d2,...dm)作为输入,其中项被打乱。程序必须按顺序打印 M 等差数列中的项,最小的开始项在前。
输入格式: 第一行包含 M 的值 第二行包含用空格分隔的术语(术语的数量将是 M*N)
边界条件: 2 = 3
输出格式: 级数中的项(每个以空格分隔)按顺序排列,具有最小起始项的级数首先出现。
输入/输出示例 1:
输入:
2
1 4 8 12 7 16
输出:
1 4 7 8 12 16
解释:
There are two progressions. Hence 6/2 = 3 terms in each progression.
So the first A.M has 1 4 7 and the second has 8 12 16
As 1 < 8, 1 4 7 is printed followed by 8 12 16
示例输入/输出 2:
输入:
3
2 6 8 10 15 22 12 11 4
输出:
2 4 6 8 15 22 10 11 12
解释:
There are three progressions. Hence 9/3 = 3 terms in each progression.
So the first A.M has 2 4 6 and the second has 8 15 22. The third has 10 11 12.
注意:我们不能将 8 10 12 作为第二级数,因为其余数字 11 15 22 不能作为算术级数。
输入/输出示例 3:
输入:
4
20 24 28 32 41 46 51 50 60 90 10 170 70 36 40 250
输出:
10 90 170 250 20 24 28 32 36 41 46 51 40 50 60 70
输入/输出示例 4:
输入:
3
180 66 100 44 120 55 60 400 200 300 33 240
输出:
33 44 55 66 60 120 180 240 100 200 300 400
我的代码(到目前为止):
from __future__ import division
from itertools import permutations
m=int(raw_input())
values=map(int,raw_input().split())
terms=len(values)/m
permutation=list(permutations(values,terms))
lst=[]
for i in range(m):
for perm in permutation:
mean=sum(perm)/len(perm)
temp=list(sorted(perm))
if temp in lst:
continue
if len(perm)%2==0:
med=(temp[int((len(temp)/2)-1)]+temp[int(len(temp)/2)])/2
if med==mean:
lst+=[temp]
else:
med=temp[int(len(temp)/2)]
if med==mean:
lst+=[temp]
lst=sorted(lst,key=lambda x:x[0])
print lst
我能够列出给定输入中所有可能的算术序列,但不知道如何从那里开始。
【问题讨论】:
标签: python algorithm list sequence