题目大意:
商品买3免1,问怎么买可以最大优惠。
解题思路:
简单排个序,三个一组,把每组最后一件商品价格相加就好了。
代码如下:
#include <stdio.h>
int P[20010];
void merge(int * a, int start, int mid, int end);
void merge_sort(int * a, int start, int end);
int main(void)
{
int t, n, i, j, tot, temp;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &P[i]);
merge_sort(P, 0, n-1);
tot = 0;
for (i = 2; i < n; i += 3)
tot += P[i];
printf("%d", tot);
if (t) printf("\n");
}
return 0;
}
void merge_sort(int * a, int start, int end)
{
int mid;
if (start < end)
{
mid = (start + end) / 2;
merge_sort(a, start, mid);
merge_sort(a, mid+1, end);
merge(a, start, mid, end);
}
}
void merge(int * a, int start, int mid, int end)
{
int i, j, k;
int n1 = mid - start + 1;
int n2 = end - mid;
int L[n1+1], R[n2+1];
for (i = 0; i < n1; i++)
{
L[i] = a[start + i];
}
for (j = 0; j < n2; j++)
{
R[j] = a[mid + 1 + j];
}
L[n1] = -1;
R[n2] = -1;
i = j = 0;
for (k = start; k <= end; k++)
{
if (L[i] > R[j]) a[k] = L[i++];
else a[k] = R[j++];
}
}