#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void arrange(int* arr, int length)
{
int tmp = 0;
for (int i = 0; i <= length-1; i++)
{
for (int j = 0;j < length - 1 - i;j++)
{
if (arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[50] = { 0 };
int length = 0;
int max;
printf(“the max weight of the ship is : “);
scanf(”%d”, &max);
printf(“the number of people is: “);
scanf(”%d”, &length);
printf(“pleese input everyone’s weight: \n”);
for (int i = 0; i < length; i++)
{
scanf("%d", &arr[i]);
}
arrange(arr, length);
int l = 0;
int r = length - 1;
int count = 0;
while (l <= r)
{
if (arr[l] +arr[r] <= max)
{
l++;
}
r–;
count++;
}
printf("%d", count);
//it’s so easy _
printf("\n");
system(“pause”);
return 0;
}
渡船问题,,贪婪算法
最新推荐文章于 2022-05-16 20:52:45 发布