贪心入门题目集

博客:https://blog.csdn.net/GreenHandCGL/article/details/89219077?utm_source=app

看电视

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef struct TVShow{
	int s, e;
}TV;

bool cmp(TV tv1, TV tv2){
	if (tv1.e == tv2.e) return tv1.s < tv2.s;//尾巴相等,按头从小到大
	return tv1.e < tv2.e;//尾巴 从小到大
} 

TV tv[105];
int main(){
	int n;
	while(scanf("%d", &n) && n != 0){
		for (int i=0; i<n; ++i){
			cin>>tv[i].s>>tv[i].e;
		}
		sort(tv, tv+n, cmp);
		int cnt = 0, pre = 0;
		for (int i=0; i<n; i++){
			if (tv[i].s >= pre){
				++cnt;
				pre = tv[i].e;
			}
		}
		cout<<cnt<<endl;
	}
	return 0;
}

出租车费

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

const int nmax=10000000+10;
const double eps=1e-6;

int main(){
	int n;
	while(1){
		cin>>n;
		if(n==0)break;
		double sum=0;
		if(n<=4)sum=10;
		else if(n>4&&n<=8)sum=10+(n-4)*2; 
		else if(n>8){//16
			sum+=18*(n/8);  //sum=18*(16/8)=18*2=36
			n=n%8;          //n=16%8=0
			if(n<=4)sum+=2.4*n;//sum=sum+2.4*n=36+2.4*0=36; 
			else sum+=10+(n-4)*2;//如果除去起步,是大于4公里的,则重新开一个 
		}
		int tmp=(int)sum;//取整 
		if(sum-tmp<eps)printf("%d\n",tmp);
		else printf("%.1f\n",sum);
	}
	return 0;
}

迷障

/*
题目大意:求解最大的解药的体积和密度
解题思路:在求解之前手算出求密度的公式
难点详解:设浓度p,质量为m,对于体积V里的物质质量为:m=p*v;如此得到总液体的总质量 M,和总体积 V;之后的浓度为: P=M/V;
关键点:理解题意
解题人:lingnichong
解题时间:2014-09-01 01:15:28
解题体会:贪心算法,注意求得是密度,还要除以100
*/
题目分析:题目意思也很直接,就是将不同浓度的混在一起,最后要求体积最大,那么可以想到用贪心做,因为体积要求最大,所以尽可能的从小浓度的开始混合,
所以要对浓度进行排序,一个一个累加,这里要注意C=n/v;混合后的浓度等于C = c1v+c2v+…/(i+1)v = c1+c2+…/(i+1)

先加浓度低的,直到浓度超过w

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


int main(){
	int c;cin>>c;
	while(c--){
		int n,v,w;
		int M[105];
		cin>>n>>v>>w;
		for(int k=0;k<n;++k) cin>>M[k];
		sort(M,M+n);
		int total=0,k;
		double p=0;
		for(k=0;k<n;k++){
			if((total+M[k])/(double)(k+1)>(double)w)break;
			total += M[k];
			p=total/(double)(k+1);
		} 
		printf("%d %.2f\n",k*v,p/100.0);
	}
	return 0;
}

均分纸牌

https://blog.csdn.net/GreenHandCGL/article/details/89219077?utm_source=app

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDouble-

你的鼓励是我最大的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值