首页 > 试题广场 >

Homework

[编程题]Homework
  • 热度指数:649 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
临近开学了,大家都忙着收拾行李准备返校,但nowcoder却不为此担心!
因为他的心思全在暑假作业上:目前为止还未开动(-_-!!还以为他有多冷静呢)。
暑假作业是很多张试卷,我们这些从试卷里爬出来的人都知道,卷子上的题目有选择题、填空题、简答题、证明题等。
而做选择题的好处就在于工作量很少,但又因为选择题题目都普遍很长。
如果有5张试卷,其中4张是选择题,最后一张是填空题,很明显做最后一张所花的时间要比前4张长很多。
但如果你只做了选择题,虽然工作量很少,但表明上看起来也已经做了4/5的作业了。 nowcoder决定就用这样的方法来蒙混过关。
他统计出了做完每一张试卷所需的时间以及它做完后能得到的价值(按上面的原理,选择题越多价值当然就越高咯)。
现在就请你帮他安排一下,用他仅剩的一点时间来做最有价值的作业。

输入描述:
测试数据包括多组。
每组测试数据以两个整数M,N(1≤M≤20, 1≤N≤10000)开头,分别表示试卷的数目和redraiment剩下的时间。
接下来有M行,每行包括两个整数T,V(1≤T≤N,0输入以0 0结束。


输出描述:
对应每组测试数据输出redraiment能获得的最大价值。
保留小数点2位
示例1

输入

4 20
4 10
5 22
10 3
1 2
0 0

输出

37.00
import java.util.Arrays;
import java.util.Scanner;

public class _t381 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while(in.hasNext()){
			int m=in.nextInt(),n=in.nextInt();
			if(m==0 && n==0) break;
			int[] times=new int[m];
			int[] vals=new int[m];
			double[] avgs=new double[m];
			for (int i = 0; i < m; i++) {
				times[i]=in.nextInt();
				vals[i]=in.nextInt();
				avgs[i]=vals[i]*1.0/times[i];
			}
			
			double dtemp=0;
			int itemp=0;
			for (int i = 0; i < m-1; i++) {
				for (int j = 0; j < m-1; j++) {
					if(avgs[j]<avgs[j+1]){
						dtemp=avgs[j];
						avgs[j]=avgs[j+1];
						avgs[j+1]=dtemp;
						
						itemp=vals[j];
						vals[j]=vals[j+1];
						vals[j+1]=itemp;
						
						itemp=times[j];
						times[j]=times[j+1];
						times[j+1]=itemp;
					}
				}
			}
//			System.out.println(Arrays.toString(avgs));
			
			int sumT=0;
			double sumV=0;
			for (int i = 0; i < m; i++) {
				if(sumT<=n){
					sumT+=times[i];
					sumV+=vals[i];
					if(i==m-1 && sumT>n){
						sumV-=vals[i];
						sumT-=times[i];
					}
				}
				else{
					sumV-=vals[--i];
					sumT-=times[i];
				}
			}
//			System.out.println(sumT);
			System.out.printf("%.2f\n",sumV);
		}
	}
}
求特殊测试用例啊啊啊啊啊啊~
发表于 2017-06-09 09:46:19 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int m = sc.nextInt();
			int n = sc.nextInt();
			if(m == 0 && n == 0) break;
			Paper[] papers = new Paper[m];
			for (int i = 0; i < m; i ++ ) {
				papers[i] = new Paper(sc.nextDouble(), sc.nextDouble());
			}
			Arrays.sort(papers);
			double sum = 0;
			for (int i = 0; i < papers.length; i ++ ) {
				if(papers[i].time > n) {
					sum += n * papers[i].perValue;
					break;
				}
				sum += papers[i].value;
				n -= papers[i].time;
			}
			System.out.printf("%.2f\n", sum);
		}
	}

	static class Paper implements Comparable<Paper> {
		double time;
		double value;
		double perValue;

		public Paper(double time, double value) {
			this.time = time;
			this.value = value;
			this.perValue = value / time;
		}

		@Override
		public int compareTo(Paper o) {
			return o.perValue > this.perValue ? 1 : - 1;
		}
	}
}
编辑于 2016-10-13 02:55:22 回复(0)
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct O {
	double t, v,rate;
};
bool cmp(O o1, O o2) {
	return o1.rate > o2.rate;
}


int main(int argc, char* argv[])
{
	int m, n, i;
	double ans = 0;
	struct O o[21];
	while(cin>>m>>n && (m || n)) {
		ans = 0;
		for(i = 0; i < m; ++i) {
			cin>>o[i].t>>o[i].v;

			o[i].rate = o[i].v/o[i].t;
		}

	/*	cout<<"输入完毕"<<endl;
	*/	sort(o, o+m, cmp);

	/*	cout<<"排序完毕"<<endl;
	*/	for(i = 0; i < m; ++i) {
			if(n - o[i].t >= 0) {
				ans += o[i].v;
				n -= o[i].t;
			}else {
				ans += n * o[i].rate;
				break;
			}
			
		}


		printf("%0.2lf\n", ans);
	}

	return 0;
}

发表于 2023-02-10 18:08:54 回复(0)
求大佬解释一下第二种为什么错??题目说了m,n大于1,我他喵找了好久发现是这个有问题   
while(cin>>m>>n && (m||n))

while(cin>>m>>n && m && n)

发表于 2018-09-04 22:07:09 回复(0)
// write your code here cpp
#include<iostream>
#include<vector>
#include<algorithm>
#include <iomanip>
using namespace std;

struct P{
int time;
int value;
double m;
};
bool cmp(P a, P b){
if (a.m - b.m > 0)
return true;
else
return false;
}

int main(){
int t, num;
while (cin >> num >> t && (num || t)){
vector<P>len(num);
for (int i = 0; i < num; i++)
{
cin >> len[i].time >> len[i].value;
len[i].m = (double)len[i].value / len[i].time;
}
sort(len.begin(), len.end(), cmp);
double sum = 0;
for (int i = 0; i < num; ++i)
{
if (t >= len[i].time)
{
sum += len[i].value;
t -= len[i].time;
}
else
{
sum += (double)len[i].m * t;
break;
}
}

cout << setiosflags(ios::fixed) << setprecision(2) << sum << endl;



}


return 0;
}
发表于 2017-04-25 19:50:47 回复(0)
T,V
T花费时间 V表示价值。
选时间短价值大的即可。
发表于 2016-02-13 01:06:14 回复(0)
贪心算法,排个序就好了。
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;

struct Paper
{
	int time;
	int value;
	double perValue;
	bool operator<(const Paper & h) const
	{
		return (h.perValue < perValue);
	}
};

int main()
{
	int M, N;
	while (cin >> M >> N && (M || N))
	{
		vector<Paper> list(M);
		for (int i = 0; i < M; ++i)
		{
			cin >> list[i].time >> list[i].value;
			list[i].perValue = list[i].value / double(list[i].time);
		}

		sort(list.begin(), list.end());
		double sum = 0;
		for (int i = 0; i < M; ++i)
		{
			if (N >= list[i].time)
			{
				sum += list[i].value;
				N -= list[i].time;
			}
			else
			{
				sum += list[i].perValue * N;
				break;
			}
		}

		cout << setiosflags(ios::fixed) << setprecision(2) << sum << endl;
	}

	return 0;
}

编辑于 2015-12-18 17:06:27 回复(0)
T和V分别是什么也不说清楚
发表于 2015-09-19 22:43:23 回复(1)

问题信息

难度:
8条回答 8405浏览

热门推荐

通过挑战的用户

查看代码