Problem H: Hard for kids

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);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值