算法笔记习题4.4

  • 问题 A: 看电视

 

题目描述

暑假到了,小明终于可以开心的看电视了。但是小明喜欢的节目太多了,他希望尽量多的看到完整的节目。
现在他把他喜欢的电视节目的转播时间表给你,你能帮他合理安排吗?

输入

输入包含多组测试数据。每组输入的第一行是一个整数n(n<=100),表示小明喜欢的节目的总数。
接下来n行,每行输入两个整数si和ei(1<=i<=n),表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。
当n=0时,输入结束。

输出

对于每组输入,输出能完整看到的电视节目的个数。

样例输入

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

样例输出

5

 

#include <cstdio>
#include <algorithm>
#include <iostream> 
using namespace std; 
struct part{
	int a;
	int b;
}part1[103];
bool cmp(part x,part y)
{
	if(x.b != y.b) return x.b<y.b;
	else return x.a>y.a;	
}
int main()
{
	int n,count,flag;
	while(scanf("%d",&n) != EOF)
	{
		if(n==0) break;
		for(int i=0;i<n;i++)
		{
			scanf("%d %d",&part1[i].a,&part1[i].b);
		}
		sort(part1,part1+n,cmp);
		count =1;
		flag=part1[0].b;
		//在while里面声明count和flag时judge判断编译错误 
		//count表示时间不交叉节目个数
		//flag表示区间端点最小值 
		for(int i=1;i<n;i++)
		{
			if(part1[i].a>=flag)
			//如果下一个节目在上一个节目结束之后
			//即刚好可以看到上个完整节目 
			{
				count ++;
				flag=part1[i].b;
			}
		} 
		printf("%d\n",count);
	} 
	return 0;
 } 

 

 

  • 问题 B: 出租车费

 

题目描述

某市出租车计价规则如下:起步4公里10元,即使你的行程没超过4公里;接下来的4公里,每公里2元;之后每公里2.4元。行程的最后一段即使不到1公里,也当作1公里计费。
一个乘客可以根据行程公里数合理安排坐车方式来使自己的打车费最小。
例如,整个行程为16公里,乘客应该将行程分成长度相同的两部分,每部分花费18元,总共花费36元。如果坐出租车一次走完全程要花费37.2元。
现在给你整个行程的公里数,请你计算坐出租车的最小花费。

 

输入

输入包含多组测试数据。每组输入一个正整数n(n<10000000),表示整个行程的公里数。
当n=0时,输入结束。

输出

对于每组输入,输出最小花费。如果需要的话,保留一位小数。

样例输入

3
9
16
0

样例输出

10
20.4
36
#include <stdio.h>
int main()
{
	int n;
	double cost;
	while(scanf("%d",&n) != EOF)
	{
		if(n==0) break;//n等于0结束 
		if(n<4)
		{
			cost=10;
		}
		else if(n>=4&&n<=8)
		{
			cost=10+(n-4)*2;
		}
		else if(n>8)
		{
			if(n%8 == 0 )
			{
				cost=n/8*18;
			}
			else if(n%8 < 5 )
			{
				cost=n/8*18 + (n%8)*2.4;
			} 
			else cost=n/8*18 + 10 + (n%8-4)*2;
		}
		if(cost==int(cost))
		{
			printf("%d\n",int(cost));
		}
		else printf("%.1f\n",cost);
	}
	return 0;
 } 

 

 

  • 问题 C: To Fill or Not to Fill

 

题目描述

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

输入

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

输出

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

样例输入

59 525 19 2
3.00 314
3.00 0

样例输出

82.89

 

//分析案例
//50 1300 12 8
//6.00 1250
//7.00 600
//7.00 150
//7.10 0
//7.20 200
//7.50 400
//7.30 1000
//6.85 300
//1.离下个一个油站/终点在600以上距离,即加满行驶最远距离
//2.在600内有油站,
//(1)600内没有比当前油站更便宜的油,加满行驶至600内相对便宜的油站 
//(2)600内有比当前油站更便宜的油,加油至恰好能行驶到 
//3.起点无油站/路线油站个数为0,车子动不了
//需要的数据:车子行驶的距离,行驶途中的花费

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
struct Gas{
	double price;//油站的油价 
	double d;//油站与杭州的距离 
}gas[510];
bool cmp(Gas a,Gas b)//比较函数 
{
	return a.d<b.d;
}
int main()
{
	int n4;
	double n1,n2,n3;
	//n1,n2,n3,n4分别为邮箱容量,终点距离,每公里消耗油量,路线油站数量 
	scanf("%lf%lf%lf%d",&n1,&n2,&n3,&n4);
	if(n4==0)//路线无油站,车子动不了,情况3 
	{
		printf("The maximum travel distance = %.2f\n",0);
		return 0;
	}
	for(int i=0;i<n4;i++)
	{
		scanf("%lf%lf",&gas[i].price,&gas[i].d);
	}
	sort(gas,gas+n4,cmp);
	if(gas[0].d != 0)//起点无油站,车子动不了,情况3 
	{
		printf("The maximum travel distance = %.2f\n",0);
		return 0; 
	}
	double dis=0;//行驶总距离 
	double cost=0;//加油总花费 
	bool tag=0;//是否到达终点 
	int nowdis=0;//当前加油站位置 
	double nowprice=gas[0].price;//当前的油价 
	double maxdis=n1*n3;//加满油能够行驶的最远距离 
	double surplus=0;//当前车子剩余的油量 
	while(!tag)
	{
		bool flag=0;//maxdis距离内有无油站 
		bool cheap=0;//maxdis距离内有无油站油价比当前低 
		double cheap_price=10000;//maxdis距离内油价较低的油站价格 
		int cheap_dis;//maxdis距离内油价较低的油站位置 
		for(int i=nowdis+1;i<n4;i++)
		{
			if((gas[i].d-gas[nowdis].d) <= maxdis)//maxdis距离内有加油站 
			{
				flag=1;
				if(nowprice>gas[i].price)//maxdis距离内有比当前油价低,情况2.(2) 
				{
					cheap=1;
					double need_gas=(gas[i].d-gas[nowdis].d)/n3 - surplus; 
					cost += need_gas * nowprice;
					surplus=0;
					nowdis=i;
					nowprice=gas[i].price;
					break;
				}
				if(gas[i].price<cheap_price)//maxdis内无油价比当前低时,找个相对较低的油站 
				{
					cheap_price=gas[i].price;
					cheap_dis=i;
				}
			}
			else break;//因为无油站时还要考虑终点的距离,故在后面考虑	
		}
		if(!cheap && (n2-gas[nowdis].d) <= maxdis)
		//在距离终点maxdis距离内找个最便宜的油站加油至恰好能到终点 
		{
			double need_gas=(n2-gas[nowdis].d)/n3 - surplus;
			cost += need_gas*nowprice;
			printf("%.2f\n",cost);
			return 0;
		}
		if(!cheap && flag)//情况2.(1) 
		{
			double need_gas=n1-surplus;
			cost += need_gas*nowprice;
			surplus=n1-(gas[cheap_dis].d-gas[nowdis].d)/n3;
			nowprice=gas[cheap_dis].price;
			nowdis=cheap_dis;
		}
		if(!flag)//情况1 
		{
			printf("The maximum travel distance = %.2f\n",gas[nowdis].d+maxdis);
			return 0;
		}
	}
	return 0;
}

 

 

  • 问题 D: Repair the Wall

 

题目描述

 

Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon.

When the typhoon came, everything is terrible. It kept blowing and raining for a long time. And what made the situation worse was that all of Kitty's walls were made of wood.

One day, Kitty found that there was a crack in the wall. The shape of the crack is 
a rectangle with the size of 1×L (in inch). Luckly Kitty got N blocks and a saw(锯子) from her neighbors.
The shape of the blocks were rectangle too, and the width of all blocks were 1 inch. So, with the help of saw, Kitty could cut down some of the blocks(of course she could use it directly without cutting) and put them in the crack, and the wall may be repaired perfectly, without any gap.

Now, Kitty knew the size of each blocks, and wanted to use as fewer as possible of the blocks to repair the wall, could you help her ?

 

 

输入

The problem contains many test cases, please process to the end of file( EOF ).
Each test case contains two lines.
In the first line, there are two integers L(0<L<1000000000) and N(0<=N<600) which
mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0<Ai<1000000000 ) means that the ith block has the size of 1×Ai (in inch).

输出

For each test case , print an integer which represents the minimal number of blocks are needed.
If Kitty could not repair the wall, just print "impossible" instead.

样例输入

2 2
12 11 
14 3
27 11 4 
109 5
38 15 6 21 32 
5 3
1 1 1

样例输出

1
1
5
impossible

 

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)//将木块体积从大到小排序 
{
	return a>b;
}
int main()
{
	int l,n,sum,count;
	int num[610];
	while(scanf("%d%d",&l,&n) != EOF)
	{
		sum=count=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
			sum += num[i];
		}
		if(sum<l)
		{
			printf("impossible\n");
		}
		else
		{
			sort(num,num+n,cmp);
			while(l>0)
			{
				l -= num[count++];	
			}
			printf("%d\n",count);
		}
	}
	return 0;
} 

 

  • 问题 E: FatMouse's Trade

 

题目描述

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain. 

 

输入

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.

 

输出

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

 

样例输入

4 2
4 7
1 3
5 5
4 8
3 8
1 2
2 5
2 4
-1 -1

样例输出

2.286
2.500

 

#include <stdio.h>
#include <algorithm>
using namespace std;
struct part{
	int a;
	int b;
	double rate;//比率,便于排序 
}part1[1001];
bool cmp(part a,part b)
{
	return a.rate>b.rate; 
} 
int main()
{
	int m,n,count;
	double sum; 
	while(scanf("%d%d",&m,&n) != EOF)
	{
		if(m==-1||n==-1) break;
		for(int i=0;i<n;i++)
		{
			scanf("%d%d",&part1[i].a,&part1[i].b);
			part1[i].rate=part1[i].a*1.0/part1[i].b;
			//注意要乘以1.0,因为比值上下为两个整数 
		}
		sort(part1,part1+n,cmp);
		count=0;
		sum=0;
		while(m>0)
		{
			if(m>part1[count].b)
			{
				sum += part1[count].a;
				m -= part1[count].b;
				count ++; 
			}
			else 
			{
				sum += m*1.0/part1[count].b*part1[count].a;
				printf("%.3f\n",sum);
				break;
			}
		}
	}
	return 0;
}

 

 

  • 问题 F: 迷瘴

 

题目描述

 

小明正在玩游戏,他控制的角色正面临着幽谷的考验——
幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅。由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死。
幸好小明早有防备,提前备好了解药材料(各种浓度的万能药水)。现在只需按照配置成不同比例的浓度。
现已知小明随身携带有n种浓度的万能药水,体积V都相同,浓度则分别为Pi%。并且知道,针对当时幽谷的瘴气情况,只需选择部分或者全部的万能药水,然后配置出浓度不大于 W%的药水即可解毒。
现在的问题是:如何配置此药,能得到最大体积的当前可用的解药呢?
特别说明:由于幽谷内设备的限制,只允许把一种已有的药全部混入另一种之中(即:不能出现对一种药只取它的一部分这样的操作)。

 

输入

 

输入数据的第一行是一个整数C,表示测试数据的组数;
每组测试数据包含2行,首先一行给出三个正整数n,V,W(1<=n,V,W<=100);
接着一行是n个整数,表示n种药水的浓度Pi%(1<=Pi<=100)。

 

输出

 

对于每组测试数据,请输出一个整数和一个浮点数;
其中整数表示解药的最大体积,浮点数表示解药的浓度(四舍五入保留2位小数);
如果不能配出满足要求的的解药,则请输出0 0.00。

 

样例输入

2
1 35 68
1 
2 79 25
59 63 

样例输出

35 0.01
0 0.00

 

#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
	int c,n,v,w,num[101],c0,count,temp;
	scanf("%d",&c);
	while(c--)
	{
		scanf("%d%d%d",&n,&v,&w);
		count=c0=0;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&num[i]);
			if(num[i]<=w)
			{
				c0 += num[i];
				//c0表示溶度之和 
				count ++;
				//count表示有多少同体积的可解毒的溶液 
			}
		}
		if(c0==0) printf("0 0.00\n");
		else 
		{
			sort(num,num+n);
			int i=0;
			for(;i<n;i++)//找到第一个刚好大于W浓度的溶液 
			{
				if(num[i]>w)
				{
					temp=i;
					break;
				}
			}
			if(i==n) printf("%d %.2f\n",v*count,c0*0.01/count);
			else 
			{
				//将一瓶大于W溶度的溶液倒入其中看总浓度是否大于W 
				while((c0+num[i])*1.0/(count+1) <= w)
				{
					c0 += num[i++];
					count ++;
				}
				printf("%d %.3f\n",v*count,c0*0.01/count);
			}
		}
	}
	return 0;
} 

 

 

  • 问题 G: 找零钱

 

题目描述

小智去超市买东西,买了不超过一百块的东西。收银员想尽量用少的纸币来找钱。
纸币面额分为50 20 10 5 1 五种。请在知道要找多少钱n给小明的情况下,输出纸币数量最少的方案。 1<=n<=99;

输入

有多组数据  1<=n<=99;

输出

对于每种数量不为0的纸币,输出他们的面值*数量,再加起来输出

样例输入

25
32

样例输出

20*1+5*1
20*1+10*1+1*2

 

#include <stdio.h>
#include <string.h>
int num[5]={50,20,10,5,1};
int hash[10];
int main()
{
	int n;
	while(scanf("%d",&n) != EOF)
	{
		memset(hash,0,sizeof(hash));
		while(n>0)
		{
			if(n-num[0]>=0)
			{
				n -= num[0];
				hash[0] ++;
			}
			else if(n-num[1]>=0)
			{
				n -= num[1];
				hash[1] ++;
			}
			else if(n-num[2]>=0)
			{
				n -= num[2];
				hash[2] ++;
			}	
			else if(n-num[3]>=0)
			{
				n -= num[3];
				hash[3]++;
			}
			else if(n-num[4]>=0)
			{
				n -= num[4];
				hash[4]++;
			}
		}
		int i=0;
		for(;i<5;i++)
		{
			if(hash[i] != 0)
			{
				printf("%d*%d",num[i],hash[i]);
				break;
			}
		}
		for(int j=i+1;j<5;j++)//由于加号的问题分开输出 
		{
			if(hash[j] != 0)
			{
				printf("+%d*%d",num[j],hash[j]);
			}
		}
		printf("\n");
	}
	return 0;
} 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值