I think it

I think it

时间: 1ms        内存:32M

描述:

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

提示:

参考答案(内存最优[0]):

#include <iostream>
using namespace std;
int main()
{
    int t=20,n,a[19]= {0};
    int i,j,k;
    while(t>10||t<1)
    {
        cin>>t;
    }
    for(k=0; k<t; k++)
    {
        cin>>n;
        while(n>9||n<2)
        {
            cin>>n;
        }
        for(i=0; i<n; i++)
        {
            cin>>j;
            while(j>=10||j<=-10)
            {
                cin>>j;
            }
            if(j>0)
                a[j+9]=j;
            if(j<0)
                a[-j]=j;
            if(j==0)
                a[j]=j;
        }
        j=0;
        for(i=18; i>=0&&j<10; i--)
        {

            if(a[i]!=0)
                j=j+a[i];
        }
        cout << j << endl;
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    int T,N;
    int i,j,k,t,b;
    int a[10];
    int sum,second;//sum为所有正数的和,second为要求的数
    cin>>T;
    for(i=1;i<=T;i++)
    {
        b=0;
        sum=0;
        cin>>N;
        for(j=0;j<N;j++)
            cin>>a[j];
        for(j=0;j<N;j++)
        {
            if(a[j]>0)
            {
                b++;
                break;
            }
        }
        if(b>0)
        {
            for(j=0;j<N;j++)
                if(a[j]!=0)
                {
                    k=abs(a[j]);
                    break;
                }                    //让k不为0,如果k一开始就为0,那之后就找不到一个比k小的正数;
            for(j=0;j<N;j++)
            {
                t=abs(a[j]);
                if(a[j]>0)
                    sum+=a[j];       //求所有正数的和
                if(t<=k&&t!=0)
                    k=t;             //使k等于最小的正数
            }
            second=sum-k;
        }
        else
        {
            for(b=0;b<N;b++)
            {
                k=b;
                for(j=b+1;j<N;j++)
                {
                    if(a[j]>a[k])
                        k=j;
                }
                t=a[b];
                a[b]=a[k];
                a[k]=t;
            }
            if(a[0]==0&&a[0]==a[1])
            {
                for(j=1;j<N;j++)
                {
                    if(a[j]<a[0])
                    {
                        second=a[j];
                        break;
                    }
                }
            }
            else if(a[0]!=a[1])
            {
                second=a[1];
            }
            else
            {
                for(j=1;j<N;j++)
                {
                    if(a[j]<a[0])
                    {
                        break;
                    }
                }
                sum=a[0]+a[1];
                if(sum>=a[j])
                    second=sum;
                else
                    second=a[j];
            }
        }
        cout<<second<<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注