算法笔记习题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 
#include 
#include  
using namespace std; 
struct part{
	int a;
	int b;
}part1[103];
bool cmp(part x,part y)
{
	if(x.b != y.b) return x.by.a;	
}
int main()
{
	int n,count,flag;
	while(scanf("%d",&n) != EOF)
	{
		if(n==0) break;
		for(int i=0;i=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 
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 
#include 
#include 
using namespace std;
struct Gas{
	double price;//油站的油价 
	double d;//油站与杭州的距离 
}gas[510];
bool cmp(Gas a,Gas b)//比较函数 
{
	return a.dgas[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

 

 

  • 问题 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 mentioned above.
In the second line, there are N positive integers. The ith integer Ai(0

输出

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 
#include 
#include 
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;i0)
			{
				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 
#include 
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;i0)
		{
			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 
#include 
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;iw)
				{
					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 
#include 
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;
} 

 

你可能感兴趣的:(算法笔记习题4.4)