Chapter IV machine experiment report

1, practice title

  Storage procedures

2. Description of the problem

 

 

 3, the algorithm description

#include<iostream> 
#include<algorithm>
using namespace std;

int main(){
    int n,L;
    cin >> n;
    cin >> L;
    
    int l[n];
    for(int i = 0;i < n; i++){
        cin >> l[i];
    }
    
    sort(l,l+n);
    
    int count = 0;
    for(int i = 0; i < n; i++){
        if(L >= l[i]){
            count++;
            L = L - l[i];
        }    
    }
    
    cout << count;
}
View Code

4, algorithm complexity analysis time and space

Time complexity: T (n) = O (n) for only one cycle

Complexity Space: S (n) = O (1) does not apply extra space

5, and experience

Greedy algorithm is simpler, easier to understand, a problem may have several different greedy algorithms, penguins enjoy are the optimal solution, and the cooperation of his teammates very happy, very happy!

 

Guess you like

Origin www.cnblogs.com/cmh2969/p/11881447.html