题目链接:
http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1012&cid=853
大体意思:
第一行表示样例的数量
第二行表示的是数组中元素的个数
第三行输入的是数组中的数
紧接着要先给数组排序一下,保证数组的顺序
在求的就是让奇数位 的数和偶数位数各自相加一下
那个位置上的数大,那个数就在前面输出
样例:
输入:
1
3
1 2 3
输出:
4 2
代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#include <iomanip>
#define ll long long
using namespace std;
ll A[1000005];
ll n,m,sum=0,a,b,sum1;
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
sum=0;
a=0;
b=1;
sum1=0;
memset(A,0,sizeof(A));
for(int i=0; i<m; i++)
{
scanf("%d",&A[i]);
}
sort(A,A+m);
for(int i=0; i<m; i++)
{
sum+=A[a];
a+=2;
if(a>m)
{
break;
}
sum1+=A[b];
b+=2;
if(b>m)
{
break;
}
}
if(sum1>sum)
{
cout<<sum1<<" "<<sum<<endl;
}
else
{
cout<<sum<<" "<<sum1<<endl;
}
}
}