I think it

题目描述
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?

输入
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),

输出
For each test case, output the answer.


样例输入
2
3
1 2 3
4
0 1 2 3
样例输出
5
5

 

 

#include<stdio.h> 
int jisuan(int a[],int n) 

    int s,ma,i,j,k,b; 
    s=ma=a[0]; 
    for(i=0; i<n; i++) 
        for(j=i; j<n; j++) 
        { 
            b=0; 
            for(k=i; k<=j; k++)b+=a[k]; 
            if(b>ma)ma=b; 
        } 
    for(i=0; i<n; i++) 
        for(j=i; j<n; j++) 
        { 
            b=0; 
            for(k=i; k<=j; k++)b+=a[k]; 
            if(b>s&&b<ma)s=b; 
        } 
    return s; 

int main() 

    int N,n,a[15],i,t,j; 
    scanf("%d",&N); 
    while(N--) 
    { 
        scanf("%d",&n); 
        for(i=0; i<n; i++)scanf("%d",&a[i]); 
        for(i=0; i<n-1; i++) 
            for(j=0; j<n-i-1; j++) 
                if(a[j]>a[j+1]) 
                { 
                    t=a[j]; 
                    a[j]=a[j+1]; 
                    a[j+1]=t; 
                } 
        printf("%d\n",jisuan(a,n)); 
    } 
    return 0; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值