1
来源:互联网 发布:淘宝秒杀器taovb 编辑:程序博客网 时间:2024/05/22 17:29
#include <iostream>
using namespace std;
int LeastBox(int V, int N, int * objects)
{
if (N < 1)
{
return 0;
}
if (N < 2)
{
return 1;
}
int box = 0;
if (objects[0] < objects[1])
{
int tmp = objects[1];
objects[1] = objects[0];
objects[0] = tmp;
}
for (int i = 2; i < N; i++)
{
for (int j = i; j > 0; j--)
{
if (objects[j] > objects[j - 1])
{
int tmp = objects[j];
objects[j] = objects[j - 1];
objects[j - 1] = tmp;
}
else
{
break;
}
}
}
int i = 0;
int j = N - 1;
while (i < j)
{
if (objects[i] + objects[j] <= V)
{
i++;
j--;
box++;
continue;
}
else
{
i++;
box++;
continue;
}
}
return box;
}
int main()
{
int V = 0;
int N = 0;
int * objects = NULL;
cin >> V;
cin >> N;
objects = new int[N];
for (int i = 0; i < N; i++)
{
cin >> objects[i];
}
cout <<LeastBox(V, N, objects) << endl;
delete [] objects;
return 0;
}
using namespace std;
int LeastBox(int V, int N, int * objects)
{
if (N < 1)
{
return 0;
}
if (N < 2)
{
return 1;
}
int box = 0;
if (objects[0] < objects[1])
{
int tmp = objects[1];
objects[1] = objects[0];
objects[0] = tmp;
}
for (int i = 2; i < N; i++)
{
for (int j = i; j > 0; j--)
{
if (objects[j] > objects[j - 1])
{
int tmp = objects[j];
objects[j] = objects[j - 1];
objects[j - 1] = tmp;
}
else
{
break;
}
}
}
int i = 0;
int j = N - 1;
while (i < j)
{
if (objects[i] + objects[j] <= V)
{
i++;
j--;
box++;
continue;
}
else
{
i++;
box++;
continue;
}
}
return box;
}
int main()
{
int V = 0;
int N = 0;
int * objects = NULL;
cin >> V;
cin >> N;
objects = new int[N];
for (int i = 0; i < N; i++)
{
cin >> objects[i];
}
cout <<LeastBox(V, N, objects) << endl;
delete [] objects;
return 0;
}
0 0
- 1
- 1
- 1
- 1
- 1
- 1
- 1》
- 1
- 1
- 1
- 1
- (1)
- 1
- 1
- 1
- 1
- 1
- 1
- 关于oracle中查询语句 decode和 case when 的用法
- 【Python】 strip(),split()函数介绍
- Get the center of contour
- Poppler ~ okular ~ qpdfview
- View和自定义控件的绘制流程
- 1
- stb前端开发js性能优化总结
- DEDECMS 完善"图片集"功能
- 基于python的hadoop开源框架
- 代理模式
- LUA面向对象程序设计(二)继承
- PKU2456二分查找
- Oracle Goldengate特别有用的几个处理数据方法
- POJ 3734 Blocks【用母函数推公式|矩阵乘法】