栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

A1113 Integer Set Partition(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【排序】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

A1113 Integer Set Partition(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【排序】

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A1​ and A2​ of n1​ and n2​ numbers, respectively. Let S1​ and S2​ denote the sums of all the numbers in A1​ and A2​, respectively. You are supposed to make the partition so that ∣n1​−n2​∣ is minimized first, and then ∣S1​−S2​∣ is maximized.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤105), and then N positive integers follow in the next line, separated by spaces. It is guaranteed that all the integers and their sum are less than 231.

Output Specification:

For each case, print in a line two numbers: ∣n1​−n2​∣ and ∣S1​−S2​∣, separated by exactly one space.

Sample Input 1:
10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:
0 3611

Sample Input 2:
13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output 2:
1 9359
代码如下:
#include
#include
#include
#include
using namespace std;
int main()
{
    int n,n1,n2,s1=0,s2=0;
    cin>>n;
    vector numbers(n);
    for(int i=0;i>numbers[i];
    if(n%2==0)
        n1=n2=n/2;
    else{
        n1=n/2;
        n2=n-n1;
    }
    sort(numbers.begin(),numbers.end());
    for(int i=0;i 
运行结果如下: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号