#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;
}
渡船问题,,贪婪算法
C语言冒泡排序与配对求解
最新推荐文章于 2021-05-19 13:30:20 发布
本文介绍了一段使用C语言实现的冒泡排序算法,该算法用于对一组整数进行排序。此外,还展示了一个解决特定问题的程序,即在给定最大载重的情况下,确定船上可以容纳多少对重量互补的人。通过用户输入,程序接收船的最大载重、人数及每个人的体重,然后使用冒泡排序算法对体重进行排序,并计算出能同时上船的配对数量。
1189

被折叠的 条评论
为什么被折叠?



