python 算术右移
Question:
题:
In mathematics, when in an arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant then it is called arithmetic constant.
在数学中,当在算术序列中是一个数字序列,使得连续项之间的差为常数时,则称为算术常数。
Eric was writing an arithmetic sequence with N terms, but while writing he mistakenly made one wrong entry and he is sure that the first term is right. Help him to find the wrong term entered by him.
埃里克(Eric)正在写一个带有N个项的算术序列,但是在写时他错误地输入了错误的单词,并且他确信第一个项是正确的。 帮助他找到他输入的错误术语。
Input:
输入:
The first line of input contains a single line T, which represents the numbers of test cases. Then the first line of each test case contains an integer N - total number of terms. And, then N space-separated integers denoting the sequence.
输入的第一行包含一行T ,代表测试用例的数量。 然后,每个测试用例的第一行包含一个整数N-项总数。 然后,用N个空格分隔的整数表示该序列。
Test Cases: 2
5
1 3 5 6 9
4
1 2 3 5
Output:
输出:
For each test case, print the wrongly entered term.
对于每个测试用例,请打印输入错误的术语。
6
5
Constraints:
限制条件:
1<=T<=100
4<=N<=1000
1<=Terms<=1000
Explanation:
说明:
In this question, the arithmetic sequence is given and we have to find the wrong input sequence number. So firstly we find the common difference between the sequence which is constant in arithmetic sequence and then check the sequence and find the wrong number. Mainly in this question the focus on the arithmetic sequence(AP).
在这个问题中,给出了算术序列 ,我们必须找到错误的输入序列号。 因此,首先我们要找到在算术序列中恒定的序列之间的共同差异,然后检查序列并找到错误的数字。 这个问题主要集中在算术序列(AP)上。
To solve this question, we are using Python3.
为了解决这个问题,我们使用Python3。
Code:
码:
# Input test cases
print("Enter test cases (T): ")
t = int(input())
while (t > 0):
t -= 1
print("Enter number of elements (N): ")
N = int(input())
print("Enter elements separated by spaces: ")
arr = list(map(int, input().split()))
# assign the values of array in different variables
a = arr[0]
b = arr[1]
c = arr[2]
d = arr[3]
# compare the difference between the array element
if (b - a == c - b): #find common difference
diff = b - a
elif(c - b == d - c):
diff = c - b
else :
diff = d - a //3;
ref = arr[0]
#compare common difference with the array elements
for i in range(1, N):
if (ref + diff == arr[i]):
ref = ref + diff
#print the wrong element in array
else :
print("Wrong term/number: ")
print(arr[i])
break
Output
输出量
Enter test cases (T):
3
Enter number of elements (N):
5
Enter elements separated by spaces:
1 3 5 8 9
Wrong term/number:
8
Enter number of elements (N):
5
Enter elements separated by spaces:
10 20 30 35 50
Wrong term/number:
35
Enter number of elements (N):
7
Enter elements separated by spaces:
11 22 33 44 55 67 77
Wrong term/number:
67
翻译自: https://www.includehelp.com/python/arithmetic-sequences-competitive-coding-questions.aspx
python 算术右移