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 算術右移