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
题意:输入-10到10内的整数,求选中其中的数,构成和的次大值。
但我个人感觉这题有错(唯一解释就是可以一个都不选,但是,题目要求:choose a part of them…老天,为何折磨),先提供几组测试用例:
5
-9 -6 -5 -4 -4 按题意思应该输出-5,但AC答案是-4
5
-1 -1 -1 -1 -1 按题意思应该输出,但AC答案是-1
所以,总结出全为负数时,最大值为0,在减去一个这些数中绝对值最小的数即可。
如上面例子 0-4=-4 …
我个人代码(没AC的):
#include<cstdio>
#include<algorithm>
//#include<windows.h>
using namespace std;
int main()
{
int T,n,i;
int a[20],b[20];
int sum,flag;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum=0;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(a,a+n);
sort(b,b+n);
for(i=0; i<n; i++)
{
//先计算存在正数的情况
if(a[i]>0)
{
//如果正数不在最后一个位置
if(i+1!=n)
{
for(int j=i+1; j<n; j++)
{
sum+=b[j];
flag = 1;
}
}
//正数在最后一个位置时
else
{
//最小为零
if(b[0]==0)
{
sum=0;
flag=1;
}
else
{
//有正有负的情况
for(int j=n-1; j>=0; j--)
{
if(b[j]<0 &&j!=n-1)
{
sum=b[n-1]+b[j];
flag=1;
break;
}
}
}
}
if(flag==1)
break;
}
//没有正数的情况
if(a[n-1]<=0)
{
//全部为零的情况
if(a[0]==0 && a[n-1]==0)
{
sum=0;
break;
}
//不是全部为零,但非负数
for(int j=n-1;j>=0;j--)
{
//全部为负数
if(b[n-1]<0)
{
for(int k=n-2;k>=0;k--)
{
if(b[k]<b[n-1])
{
// -9 -6 -5 -4 -4 与-9 -6 -5 -1 -1
sum=max(b[k],b[n-1]+b[n-2]);
//printf("sum=%d** ",sum);
flag=1;
break;
}
//全为负数且相同
if(b[0]==b[n-1])
{
sum=b[n-2]+b[n-1];
//printf("sum=%d** ",sum);
flag=1;
break;
}
}
}
if(flag==1)
break;
//有零,即负数不在最后一位
if(b[j]<0 &&j!=n-1)
{
sum=b[j];
flag=1;
//printf("%d****",j);
break;
}
}
if(flag==1)
break;
}
}
printf("%d\n",sum);
}
return 0;
}
AC代码(2种):
1.即上面的绝对值思想:
#include<stdio.h>
#include<math.h>
int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int i,x;
int min=10;
int sum=0;
for(i=0; i<n; i++)
{
scanf("%d",&x);
if(x>0)
sum+=x;
if(fabs(x)<min&&x!=0)
min=(int)fabs(x);
}
printf("%d\n",sum-min);
}
return 0;
}
2.暴力遍历(即每种情况的和算出来,先求最大,再求次大)
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int T,n;
int a[20];
int sum,sum1,sum2;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
sum1 = 0;
sum=sum2=a[0];
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
sum1=0;
for(int k=i;k<=j;k++)
{
//printf("%d--%d***\n",i,j);
sum1+=a[k];
}
//找到最大值
sum=max(sum,sum1);
}
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
sum1=0;
for(int k=i;k<=j;k++)
sum1+=a[k];
//即找到次大值
if(sum1>sum2 &&sum1<sum)
sum2=sum1;
}
}
printf("%d\n",sum2);
}
return 0;
}