乘船问题

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

const int maxn = 1111l;

int n;

int c;

int a[maxn];

int main()
{

while (scanf("%d%d", &n, &c) != EOF && n != 0)
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
sort(a, a + n);
int i = 0;
int j = n;
if (a[j] > c)
{
printf("-1 ");
continue;
}
int cnt = 0;
while (i < j)
{
if (a[i] + a[j] <= c)
{
i++;
j--;
cnt++;
}
else
{
j--;
}
}
if (i == j)
{
cnt++;
}
printf("%d ", cnt);
}
system("pause");
return 0;
}

原文地址:https://www.cnblogs.com/SSYYGAM/p/4212500.html