如何用python做WOJ-1074这道题

Given an array consists of n positive integers,your task is to find the largest C which makes C=A+B. A,B,C are all in the given array.

输入格式
Standard input will contain multiple test cases. The first line of the input is an integer T (1 <= T <= 20) which is the number of test cases. For each test case,there is an integer n (3<=n<=10000) in the first line,indicating the number of elements in the array.The next line contains n positive integers.You can assume all the integers will be less then 10^9.

输出格式
For each test case,if you can find the largest integer C=A+B, output C. Notice, A and B should be different elements in the array.If there isn't such integer C,output -1 instead.

样例输入
3
10
1 1 1 4 5 5 6 6 10 9
3
5 5 10 (这里有个坑,10后面有个空格,导致我一直找半天,最终明白是他这个地方输入的有问题)
3
1 3 6
样例输出
10
10
-1

  1. //用两个指向数组的指针,一个从前往后扫描,一个从后往前扫描,

  2. //记为first和last,如果 fist + last < sum 则将fist向前移动,如果fist + last > sum,则last向后移动。

# 1074.py
def main():
    t=int(input())
    while t>0:
        t=t-1
        n=int(input())
        summ=0;
        a=input()
        #有可能是第二个例子里面输入的数字后面多输了一个空格,导致程序出现错误
        l=len(a)
        g=a.rstrip()
        c=[]
        b=g.split(" ")
        for bb in b:
            c.append(int(bb))
        d=sorted(c)
        #print(d)
        first=[]
        for i in range(0,n):
            first.append(0)
        last=d
        for i in range(0,n):
            first[n-1-i]=d[i];
        print(maxx(first,last,n))

def maxx(first,last,n):
    summ=0
    j=0
    k=0
    for i in range(0,n):
        summ=first[i]
        j=i+1
        k=0
        while j<n and k<n-1-i-1:
            if first[j]+last[k]<summ:
                k=k+1
            elif first[j]+last[k]>summ:
                j=j+1
            elif first[j]+last[k]==summ:
                return summ
    return -1            
# main()
if __name__ == "__main__":
    main()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值