PTA:人以群分 (25分) (C语言版)

​5
​​ ）。随后一行给出N个正整数，分别是每个人的活跃度，其间以空格分隔。题目保证这些数字以及它们的和都不会超过2
​31
​​ 。

Outgoing #: N1
Introverted #: N2
Diff = N3

10
23 8 10 99 46 2333 46 1 666 555

Outgoing #: 5
Introverted #: 5
Diff = 3611

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

Outgoing #: 7
Introverted #: 6
Diff = 9359

#include <stdio.h>
#include <stdlib.h>

int comfunc(const void *elem1, const void *elem2);
void sort_character(int *p, int n);

int main()
{
int n, i;
int a[100001];

scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
qsort(a, n, sizeof(int), comfunc);
sort_character(a, n);

return 0;
}

int comfunc(const void *elem1, const void *elem2)
{
int *p1 = (int*)elem1;
int *p2 = (int*)elem2;

return *p1 - *p2;
}
void sort_character(int *p, int n)
{
int i, j, n1, n2, sum1, sum2, dif, dif1, dif2;

sum1 = sum2 = 0; dif = dif1 = dif2 = 0;
if (n % 2 == 0)
{
n1 = n2 = n / 2;
for (i = 0; i < n1; i++)
sum1 += p[i];
for (i = n1; i < n; i++)
sum2 += p[i];
dif = abs(sum2 - sum1);
}
else
{
n1 = n2 = n / 2;
//		 dif1 = abs(p[n1] - p[n1 - 1]);          //总活跃度差值！！！ 不是两个之间之差。
//		 dif2 = abs(p[n2] - p[n2 + 1]);
for (i = 0; i < n1; i++)
sum1 += p[i];
for (i = n / 2 + 1; i < n; i++)
sum2 += p[i];
dif1 = abs(sum1 + p[n1] - sum2);
dif2 = abs(sum2 + p[n2] - sum1);
dif = (dif1 > dif2) ? dif1 : dif2;
if (dif1 > dif2)
n1++;
else
n2++;
}
printf("Outgoing #: %d\n", n2);
printf("Introverted #: %d\n", n1);
printf("Diff = %d\n", dif);

}


©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客