每天打卡一小时 第三十天 贪心算法

 

#include<bits/stdc++.h>
using namespace std;

int findContentChildren(vector<int>& children, vector<int>& cookies);

int main()
{
    int n;
    cin>>n;
    vector<int> children(n);
    for(int i=0;i<n;i++)
    {
        if(!(cin>>children[i]))
		{
            cout<<"invalid input"<<endl;
            return 0;
        }
    }
	
    int m;
    cin>>m;
    vector<int> cookies(m);
    for(int i=0;i<m;i++)
    {
        if(!(cin>>cookies[i]))
		{ 
            cout<<"invalid input"<<endl;
            return 0;
        }
    }
    
    cout<<findContentChildren(children, cookies);
	
} 

int findContentChildren(vector<int>& children, vector<int>& cookies)
{
	sort(children.begin(), children.end());
	sort(cookies.begin(), cookies.end());
	int child = 0, cookie = 0;
	while (child < children.size() && cookie < cookies.size())
	{
		if (children[child] <= cookies[cookie]) 
		    ++child;
		else
		    ++cookie;
	}
	return child;
}

  

posted @ 2023-05-18 22:20  财神给你送元宝  阅读(16)  评论(0编辑  收藏  举报