python 算术右移_Python算术序列| 竞争编码问题

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值