OJ-1020 I think it

Description

Xiao Ming is only seven years old, Now I give him some numbers, and ask him what is the second largest sum if he can choose a part of them. For example, if I give him 1 、 2 、 3 , then he should tell me 5 as 6 is the largest and 5 is the second. I think it is too hard for him, isn ’ t it?

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <=10) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case starts with a line containing an integer N (1<N<10) , the number I give Xiao Ming . The second line contains N Integer numbers ai (-10<ai<10),

Output

For each test case, output the answer.

Sample Input

2
3
1 2 3
4
0 1 2 3

Sample Output

5
5

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int m,n,i,j,k;
    int sum,sum1,sum2;
    int a[100];
    cin>>m;
    while(m--)
    {
        cin>>n;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
        }
        sort(a,a+n);
        sum=a[0];sum1=0;sum2=a[0];
        for(i=0;i<n;i++)
        {
            for(j=i;j<n;j++)
            {
                sum1=0;
                for(k=i;k<=j;k++)
                {
                    sum1+=a[k];
                }
                if(sum1>sum)
                    sum=sum1;
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=i;j<n;j++)
            {
                sum1=0;
                for(k=i;k<=j;k++)
                {
                    sum1+=a[k];
                }
                if(sum1>sum2&&sum1<sum)
                    sum2=sum1;
            }
        }
        cout<<sum2<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wangws_sb/article/details/80759900
今日推荐