1. 题目:
2. 理解:
好了先创建一个磁带,然后最程序占用的空间进行排序,从小到大排,然后一个个放进去磁带,放的进就继续放,放不进就返回程序数目。
3. 贴代码:
#include <iostream> #include<algorithm> #include<cstring> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(int argc, char** argv) { int num, length; cin >> num >> length; int number[num]; for (int i = 0; i < num; i ++){ cin >> number[i]; } int count = 0; int k = 0; sort(number, number + num); for (int n = 0; n < num; n++){ int z = length - count; if (number[n] > z) break; else{ count += number[n]; k ++; } } cout << k; return 0; }
4. 时间复杂度:自己理解了下,用系统的sort函数排序的时间复杂度是nlogn,for循环的复杂度是n,所以时间复杂度应该是nlogn。
5. 感想:感觉一起合作打题目还是很有效率的吧,自己不懂的地方说不定别人就有思路了。