C++基础算法——贪心算法

思想:总是做出在当前看来是最好的选择

例题一、排队打水问题

n个人,r个水龙头,花费时间最少的安排?(包含等待时间)

cpp 复制代码
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,r,s=0;
	cin>>n>>r;
	int a[510]={0};
	for (int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	//将数组a[]从小到大排序,需要用头文件#include <bits/stdc++.h>
	/*for (int i=1;i<=n;i++){
		cout<<a[i]<<" ";
	}*///输出:2 4 6 5
	for(int i=1;i<=n;i++){
		if(i>=r+1)a[i]=a[i]+a[i-r];
		s+=a[i];
	}
	cout<<s;
	return 0;
}

例题二、分发饼干(LeetCode455)

假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。

对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j,都有一个尺寸 s[j] 。如果 s[j] >= g[i],我们可以将这个饼干 j 分配给孩子 i ,这个孩子会得到满足。你的目标是满足尽可能多的孩子,并输出这个最大数值。

cpp 复制代码
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
	int g[500]={0};			//孩子
	int s[500]={0};			//饼干
	int i,j,n=0;				//n是满足的个数
	cin>>i>>j;
	for(int k=0;k<=i-1;k++)cin>>g[k];
	for(int k=0;k<=j-1;k++)cin>>s[k];
	sort(s,s+j);
	sort(g,g+i);
	int index=j-1;
	for(int k=i-1;k>=0;k--){
		if(index>=0&&s[index]>=g[k]){
			n++;
			index--;
		}
	}
	cout<<n;
	return 0;
}
/*
输入1:
2 3
1 2
1 2 3
输出1:
2

输入2:
4 4
1 2 7 10
1 3 5 9
输出2:
3
*/

例题三、最大子数组和

53. 最大子数组和

给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

子数组是数组中的一个连续部分。

cpp 复制代码
#include<iostream>
using namespace std;
int main(){
	int n=0,result=0,count=0;
	int a[500]={0};
	int j=0;
	do{
		cin>>a[j];
		j++;
	}while(getchar()!='\n');
	n=j;
	for(int i=0;i<=n;i++){
		count+=a[i];
		if(count>result)result=count;
		if(count<0)count=0;
	}
	cout<<result<<endl;
	return 0;
}

例题四、买卖股票的最佳时机 II

122. 买卖股票的最佳时机 II - 力扣(LeetCode)

cpp 复制代码
#include<iostream>
using namespace std;
int main(){
	int n=0,sum=0;
	int a[500]={0};
	int j=0;
	do{
		cin>>a[j];
		j++;
	}while(getchar()!='\n');
	n=j;
	for(int i=1;i<=n;i++){
		sum+=max(a[i]-a[i-1],0);
	}
	cout<<sum<<endl;
	return 0;
}
/*
输入:7 1 5 10 3 6 4
输出:12
*/

例题五、跳跃问题I

55. 跳跃游戏 - 力扣(LeetCode)

思路:看cover面积是否超过即可!

cpp 复制代码
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	int n,result=0,count;
	cin>>n;
	int a[500]={0};
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<=n;i++){
		count+=a[i];
		if(count>result)result=count;
		if(count<0)count=0;
	}
	cout<<result<<endl;
	return 0;
}

例题六、跳跃问题II

45. 跳跃游戏 II - 力扣(LeetCode)

cpp 复制代码
#include<iostream>
#include<cstdio>
#include <bits/stdc++.h>
using namespace std;
int main(){
	int result=0,next=0,cur=0;
	int num[10001]={0};
	int i=0;
	do{
		cin>>num[i];
		i++;
	}while(getchar()!='\n');
	int j;
	j=i;
	for(i=0;i<=j;i++){
		next = max(next,i+num[i]);
		if(i==cur){
			result++;
			cur=next;
			if(next>=j-1)break;
		}
	}
	cout<<result<<endl;
	return 0;
}

例题七、K次取反后最大化的数组和

1005. K 次取反后最大化的数组和 - 力扣(LeetCode)

cpp 复制代码
#include<iostream>
#include<cstdio>
#include <bits/stdc++.h>
using namespace std;
int main(){
	int a[10001];
	int K,i=0;
	do{
		cin>>a[i];
		i++;
	}while(getchar()!='\n');
	int j=i;//j是数组元素个数
	cin>>K;
	sort(a,a+j);//将所有数从小到大排列
	int negative=0;//记录负数的个数
	for(i=0;i<=j-1;i++){
		if(a[i]<0)negative++;
	}
	if(K<=negative){//反转次数小于负数个数,则将负数从最小开始反转
		for(i=0;i<=K-1;i++)a[i]=-a[i];
	}
	if(K>=negative){//反转次数大于负数个数,则将所有负数反转
					//再将数组重排,反转最小非负数即可
					//(剩余次数为偶数不需改变,为奇数将a[0]反转即可!
		int x=0;
		while(a[x]<0){
			a[x]=-a[x];
			x++;
		}
		sort(a,a+j);
		if((K-negative)%2==1)a[0]=-a[0];
	}
	
	int sum=0;//求和
	for(i=0;i<=j-1;i++){
		//cout<<a[i]<<" ";
		sum+=a[i];
	}
	cout<<sum;
	return 0;
}

例题八、加油站

力扣题目链接

cpp 复制代码
#include<iostream>
#include<cstdio>
#include <bits/stdc++.h>
using namespace std;
int main(){
 	int gas[10001]={0};
	int cost[10001]={0};
	int ii,jj=0;
	do{
		cin>>gas[ii];
		ii++;
	}while(getchar()!='\n');
	do{
		cin>>cost[jj];
		jj++;
	}while(getchar()!='\n');
	int i=ii;				//i用来表示gas和cost数组的元素个数
	int cursum=0,totalsum=0,start=0;
	for(int k=0;k<=i-1;k++){
		cursum+=gas[k]-cost[k];
		totalsum+=gas[k]-cost[k];
		if(cursum<0){
			start=k+1;
			cursum=0;
		}
	}
	if(totalsum<0)return -1;
	return start;
}

**例题九、**分发糖果

力扣题目链接

cpp 复制代码
#include<iostream>
#include<cstdio>
#include <bits/stdc++.h>
using namespace std;
int main(){
 	int rating[10001];
 	int candy[10001];
 	std::fill(std::begin(candy), std::end(candy), 1);
 	int x=0;
 	do{
 		cin>>rating[x];
 		x++;
	}while(getchar()!='\n');
	for(int i=0;i<=x-1;i++){
		if(rating[i+1]>rating[i])candy[i+1]=candy[i]+1;
		cout<<candy[i]<<" ";
	}
	for(int i=x-1;i>=0;i--){
		if(rating[i-1]>rating[i])
			candy[i-1]=max(candy[i-1],candy[i]+1);
	}
	int sum=0;
	for(int i=0;i<=x-1;i++){
		cout<<candy[i]<<" ";
		sum+=candy[i];
	}
	cout<<sum<<endl;
	return 0;
}
/*
输入:1 2 2 5 4 3 2
输出:1 2 1 2 1 1 1 
	  1 2 1 4 3 2 1 
	  14*/

例题十、柠檬水找零

力扣题目链接

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int bill[10001];
	int five=0,ten=0,twenty=0;
	std::fill(std::begin(bill),std::end(bill),0);
	int n=0;
	do{
		cin>>bill[n];
		n++;
	}while(getchar()!='\n');
	for(int i=0;i<=n-1;i++){
		if(bill[i]==5)five++;
		if(bill[i]==10){
			if(five>0){
				five--;ten++;
			}else {
				cout<<"false"<<endl;
				return 0;
			}
		}
		if(bill[i]==20){
			if(ten>0&&five>0){
				ten--;five--;twenty++;
			}else if(five>=3){
				five-=3;twenty++;
			}else {
				cout<<"false"<<endl;
				return 0;
			}
		}
	}
	cout<<"true"<<endl;
	return 0;
}

用return 0结束程序

相关推荐
枫の准大一1 小时前
【C++游记】物种多样——谓之多态
开发语言·c++
熬了夜的程序员1 小时前
【LeetCode】30. 串联所有单词的子串
算法·leetcode·链表·职场和发展·深度优先
JuneXcy3 小时前
循环高级(1)
c语言·开发语言·算法
Ka1Yan4 小时前
什么是策略模式?策略模式能带来什么?——策略模式深度解析:从概念本质到Java实战的全维度指南
java·开发语言·数据结构·算法·面试·bash·策略模式
绝无仅有5 小时前
Go Timer 面试指南:常见问题及答案解析
后端·算法·架构
地平线开发者6 小时前
开发者说|H-RDT:基于人类操作数据的跨本体机器人学习
算法·自动驾驶
biuyyyxxx7 小时前
Excel数组学习笔记
笔记·学习·算法
南莺莺8 小时前
//Q是一个队列,S是一个空栈,实现将队列中的元素逆置的算法。
数据结构·算法·链表·
闻缺陷则喜何志丹8 小时前
【分治法 BFS 质因数分解】P12255 [蓝桥杯 2024 国 Java B] 园丁|普及+
c++·算法·蓝桥杯·宽度优先·质因数分解·分治法
寒冬没有雪8 小时前
按对角线进行矩阵排序
c++·算法