Problem H: Hard for kids

商燕七
2023-12-01

Problem H: Hard for kids

Time Limit: 1 Sec   Memory Limit: 32 MB
Submit: 35   Solved: 4
[ Submit][ Status][ Web Board]

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

HINT

这一道题,纯属考你的考虑是否周全!我的妈妈,出了考场之后


发现只考虑了正数和负数,把0忘了!出了考场完善代码想把0考


虑进去发现行不通!最后问老师要测试数据,老师给我说了几个


关键数据发现有一个没过 0 0 0 -1,我的个去!发下现在的AC代


#include <cstdio>
#include<string.h>
#include <algorithm>
using namespace std;
int main()
{
    int times,n,i,f,sum2,flag,k,l,j;
    int a[15];
    scanf("%d",×);
    while (times--)
    {
        memset(a,0,sizeof(a));
        f=0;
        sum2=0;
        scanf("%d",&n);
        for (i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a+1,a+n+1);
        //              for (i=1;i<=n;i++)
        //                      {
        //                      printf("%d ",a[i]);
        //                      }
        for (i=1;i<=n;i++)
        {
            if (a[i]<0)
            {
                f=i;
            }
            else
            {
                break;
            }
        }
        //  printf("%d %d\n",f,n);
        if (f==0)
        {
            for (i=1,j=0;i<=n;i++)
            {
                if(a[i]==0)
                {
                    j=i;
                }
                else
                {
                    sum2+=a[i];
                }
            }
            printf("%d\n",sum2-a[j+1]);
        }
        else if (f==n)
        {
            //  printf("%d\n",f);
            printf("%d\n",a[n-1]);
        }
        else if (f==n-1)
        {
            printf("%d\n",a[n]+a[n-1]);
        }
        else
        {
            flag=f+1;
            //  printf("%d\n",flag);
            for (i=flag,j=0;i<=n;i++)
            {
                if(a[i]==0)
                {
                    j=i;
                }
                else
                {
                    sum2+=a[i];
                }
            }
            if(j==0)
            {
                if (sum2+a[f]>sum2-a[flag])
                {
                    printf("%d\n",sum2+a[f]);
                }
                else
                {
                    printf("%d\n",sum2-a[flag]);
                }
            }
			else if (j==n)
			{
				printf("%d\n",a[f]);
			}
            else
            {
                if (sum2+a[f]>sum2-a[j+1])
                {
                    printf("%d\n",sum2+a[f]);
                }
                else
                {
                    printf("%d\n",sum2-a[j+1]);
                }
            }
			
        }
    }
}

发一下学长的AC代码,写的很好!

#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int times,n,a[15],i,j,k,ans,maxx,sec_max;
    scanf("%d",×);
    while(times--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        sec_max=maxx=a[0];
        for(i=0;i<n;i++)
        {
            for(j=i;j<n;j++)
            {
                ans=0;
                for(k=i;k<=j;k++)
                {
                    ans+=a[k];
                }
                if(ans>maxx)
                {
                    maxx=ans;
                }
            }
        }

        for(i=0;i<n;i++)
        {
            for(j=i;j<n;j++)
            {
                ans=0;
                for(k=i;k<=j;k++)
                {
                    ans+=a[k];
                }
                if(ans>sec_max)
                {
                   if(ans<maxx)
                   {
                        sec_max=ans;
                   }
                }
            }
        }
        printf("%d\n",sec_max);
    }
}


 类似资料:

相关阅读

相关文章

相关问答