2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Before an Exam

Before an Exam

时间:2021-06-08 18:45:41

相关推荐

Before an Exam

传送门

【问题描述】

Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter’s strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.

So, today is the day when Peter’s parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.

【输入格式】

The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.

【输出格式】

In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents’ instructions; or print NO in the unique line. If there are many solutions, print any of them.

【样例输入1】

1 485 7

【样例输出1】

NO

【样例输入2】

2 50 13 5

【样例输出2】

YES1 4

【订正bug】

//以下是小新的代码,结果只拿了80分,显示最后一个点的结果错误,请你帮忙订正#include<bits/stdc++.h>using namespace std;int mn[40],mx[40],ans[40];int main(){int a,b,smin=0,smax=0,r=0;cin>>a>>b;for(int i=1;i<=a;i++){cin>>mn[i]>>mx[i];smin+=mn[i];smax+=mx[i];}if(smin<=b && smax>=b){cout<<"YES"<<endl;r=b;int ts=1;while(r>mx[ts]){cout<<mx[ts]<<" ";r-=mx[ts];ts++;}cout<<r<<endl;}elsecout<<"NO"<<endl;for(int i=1;i<=a;i++)for(int j=mn[i];j<=mx[i];j++)ans[i]=j;return 0;}

【正解】

题目分析:题目也不是很难,大意是:Peter在 d 天之后有一场考试,他父母给他规定了一个复习的总时间Sum,Peter要自己制定一个学习计划表,其中每一天都要包含这天学习的最少时间Mini 和最大时间 Maxi ,每一天学习时间 Mini<= timei <=Maxi 是否能达到父母的规定,既在d天里学习时间总和为Sum。

不能达到输出“NO”;能达到输出“YES”和每天的学习时间(一种情况即可)。

思路:知道每天的最小时间和最大时间,那么 如果最小时间总和大于Sum,或者最大时间总和小于Sum,那么不能达到要求。如果Sum在最小值总和与最大值总和之间,那么符合;可以先比较最小值总和与Sum的大小关系,如果相等,那么输出即可,如果不等,那么从前至后依次补充时间数,直到满足为止,注意,最后补充的那一天要特殊处理;而后输出即可;

#include<iostream>using namespace std;int main(){int i,d,sum,s=0,w=0;int m[31],n[31],t[31];cin>>d>>sum;for(i=1;i<=d;i++) //输入每天的Min和MAx,并算出最小值总和和最大值总和{cin>>m[i]>>n[i];s+=n[i];w+=m[i];}if(w>sum||s<sum)cout<<"NO";if(w<=sum&&s>=sum){cout<<"YES"<<endl;//最小值总和与Sum相等的情况if(w==sum)for(i=1;i<=d;i++)cout<<m[i]<<" ";else {for(i=1;i<=d;i++)//把第i天可以补充的时间存起来{t[i]=n[i]-m[i];}int p=1;int e=m[1];while (w+t[p]<=sum) //从前往后补充时间{m[p]=n[p];w+=t[p];p++;}m[p]=m[p]+sum-w; for(i=1;i<=d;i++){cout<<m[i];if(i!=d)cout<<" ";}}}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。