独木舟上的旅行

独木舟上的旅行_第1张图片


#include
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++;
        }
        else
            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;
}

你可能感兴趣的:(c语言,acm,c语言)