首页 > 试题广场 >

等差数列

[编程题]等差数列
  • 热度指数:36475 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
如果一个数列S满足对于所有的合法的i,都有S[i + 1] = S[i] + d, 这里的d也可以是负数和零,我们就称数列S为等差数列。
小易现在有一个长度为n的数列x,小易想把x变为一个等差数列。小易允许在数列上做交换任意两个位置的数值的操作,并且交换操作允许交换多次。但是有些数列通过交换还是不能变成等差数列,小易需要判别一个数列是否能通过交换操作变成等差数列

输入描述:
输入包括两行,第一行包含整数n(2 ≤ n ≤ 50),即数列的长度。
第二行n个元素x[i](0 ≤ x[i] ≤ 1000),即数列中的每个整数。


输出描述:
如果可以变成等差数列输出"Possible",否则输出"Impossible"。
示例1

输入

3
3 1 2

输出

Possible
python有个自动排序,好理解一点
a=eval(input())
b=list(input().split(" "))
for i in range(len(b)):
    b[i]=eval(b[i])
b.sort()
num=b[1]-b[0]
n=1
for i in range(len(b)-1):
    if (b[i+1]-b[i])==num:
        n+=1
        continue
    else:
        print("Impossible")
        break
if n==len(b):
    print("Possible")

发表于 2019-10-28 22:33:54 回复(0)
n=input()
n=int(n)
str=input()
str=str.split(' ')
for i in range(n):
    str[i]=int(str[i])
total_sorted=[]
count=0
str.sort()
for j in range(n-2):
    if str[j+1]-str[j]==str[j+2]-str[j+1]:
        count=1
    
str.sort(reverse=True)
for j in range(n-2):
    if str[j]-str[j+1]==str[j+1]-str[j+2]:
        count=1
if count==0:
    print("Impossible")
if count==1:
    print("Possible")


发表于 2019-09-06 11:05:39 回复(0)
最简单易懂的Python解法:
def judge(array,n):
    listhere = sorted(array)
    if n == 2:
        return True
    elif n == 3:
        return (listhere[1]-listhere[0]) == (listhere[2]-listhere[1])
    else:
        count = 0
        for i in range(0,n-2):
            if (listhere[i+1] - listhere[i]) == (listhere[i+2] - listhere[i+1]):
                count += 1 
        if count == n-2:
            return True
        else:
            return False     

def main():
    n = int(input())
    array = [int(i) for i in input().strip().split()]
    if judge(array,n):
        print('Possible')
    else:
        print('Impossible')
if __name__ == '__main__':
    main()
发表于 2019-08-13 20:43:31 回复(0)
n = int(input())
S = list(map(int,input().split()))
S.sort()
S_sub = []
for i in range(1,len(S)):     
    S_sub.append(S[i] - S[i-1])
S_sub_set = set(S_sub)
if len(S_sub_set) == 1:
    print("Possible")
else:
    print("Impossible")
编辑于 2019-05-15 14:46:40 回复(0)
m=input()
n=input()
n=[int(i) for i in n.split()]
n.sort()
d=n[1]-n[0]
a1=n[0]
t=True
for i in range(len(n)):
    if n[i]!=a1+(i-0)*d:
        t=False
        break
if t:
    print('Possible')
else:
    print('Impossible')

等差数列公式an=a1+(n-1)d

发表于 2019-04-24 00:27:14 回复(0)
length, array = input(), list(map(int, input().split()))
if length == 2:
    print("Possible")
else:
    array.sort()
    if array[1]-array[0] ==array[2]-array[1]:
        print("Possible")
    else:
        print("Impossible")

1.首先判断数组的长度,若长度为2则为等差数组
2.若长度大于2则对其排序,利用等差数组的性质(相邻两者的差相等)就可以判断了。

发表于 2019-04-22 21:28:05 回复(0)
运行时间:27ms     占用内存:3680k
import sys
n=int(sys.stdin.readline().strip())
A=list(map(int,sys.stdin.readline().strip().split(' ')))
A.sort()
d=A[1]-A[0]
for i in range(2,n):
    if A[i]-A[i-1]!=d:
        print('Impossible')
        break
else:
    print('Possible')

发表于 2019-03-30 22:37:55 回复(0)
n = int(input())
x = list(map(int, input().split()))
x.sort()
d = 0
flag = 0
sum = 0
for i in range(n-1):
    d = x[i] - x[i+1]
    sum += d

if sum/(n-1) == d:
    print('Possible')
else:
    print('Impossible')

发表于 2019-03-28 11:17:28 回复(0)
#依据求和公式判断
if __name__ == "__main__":
    n = int(input())
    x = list(map(int, input().strip().split()))
    if sum(x) == (min(x)+ max(x))*len(x)/2:
        print("Possible")
    else:
        print("Impossible")

发表于 2019-03-19 15:52:28 回复(0)
##Python

n=int(input())
s=list(map(int,input().split()))
s.sort()
diff=[]
for i in range(n-1):
    diff.append(s[i+1]-s[i])
if len(set(diff))==1:
    print("Possible")
else:
    print("Impossible")

发表于 2019-03-16 19:57:08 回复(0)

python两行

length, arr = int(input()), sorted(list(map(int, input().split())))
print("Possible" if len(set(map(lambda c: arr[c + 1] - arr[c], range(length - 1)))) == 1 else "Impossible")

思路:

  1. 先对输入的数组进行排序。
  2. 记录数组中两两相邻的差值,将差值放到一个数组里。
  3. 如果是等差数列,那么这个差值全部为同一个数。既len(set(该数组)) == 1。
编辑于 2019-02-24 15:39:36 回复(6)
# coding=utf-8

def fun():
    n = int(raw_input())
    x = map(int,raw_input().split())
    x = sorted(list(x))
    if n==2:
        return 'Possible'
    for i in range(n-2):
        if x[i+1]-x[i] != x[i+2]-x[i+1]:
            return 'Impossible'
    return 'Possible'

if __name__=='__main__':
    print(fun())

发表于 2019-01-15 21:25:53 回复(0)
length = int(raw_input())

List = []
List = raw_input().split(' ')
for i in range(length):
    List[i] = int(List[i])
     
List.sort()
count = 0
 
for i in range(length-2):
    if List[i+1] - List[i] == List[i+2] - List[i+1]:
        count += 1
        continue
    else:
        print "Impossible"
        break
if count == int(length) - 2:
    print "Possible"

编辑于 2017-09-04 23:14:03 回复(0)