方法1:
--------------------------------------------------------------------------------
#include<stdio.h>
int sum;
void f(int *a, int w, int n)
{
int i = 0, j = 1, temp = 0, t, temp1, s;
for (i = 0; i < n; i++)
{
s = 0, temp1 = 0;
for (j = i + 1; j < n; j++)
{
if (a[i] + a[j] <= w && a[j] > temp1)
{
t = j;
temp1 = a[t];
s = 1;
}
}
if(s == 1)
{
temp = a[n - 1]; a[n - 1] = a[t]; a[t] = temp;
n--;
}
sum++;
}
}
int main()
{
int s, w, i, n, a[300];
scanf("%d",&s);
while (s--)
{
sum = 0;
scanf("%d%d",&w, &n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
f(a, w, n);
printf("%d\n",sum);
}
return 0;
}
--------------------------------------------------------------------------------
方法2:
--------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int sort(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main()
{
int n, sum;
scanf("%d",&n);
while (n--)
{
int count = 0, num;
scanf("%d%d",&sum,&num);
int arr[3000], i, j;
for(i = 0; i < num; i++)
scanf("%d",&arr[i]);
qsort(arr, num, sizeof(int),sort);
for (i = 0, j = num-1; i <= j;)
{
if ( arr[i] + arr[j] <= sum )
{
count++;
i++;
j--;
}
else if (i == j)
{
count++;
break;
}
else
{
count++;
j--;
}
}
printf("%d\n",count);
}
return 0;
}
--------------------------------------------------------------------------------