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

A watermeten 《Before an Exam》

时间:2023-01-24 17:10:32

相关推荐

A watermeten 《Before an Exam》

这是第二次参加的网络比赛,虽然是内部的,但也体会了一把比赛的感觉;题目比上次简单多了,会做的也不少,现在把其中我做的问题拿出来分析一下: A. 《Watermelon》 time limit per test 1 second memory limit per test64 megabytes input standard input output standard output

One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showedwkilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.

Input

The first (and the only) input line contains integer numberw(1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys.

Output

PrintYES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; andNOin the opposite case.

Sample test(s) input

8

output

YES

Note

For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant — two parts of 4 and 4 kilos).

这个问题实在太简单了,当然是在能理解题意的前提下,呵呵,我也是用了很长时间才翻译过来。

题意:两个人买了一个西瓜,两个人想把西瓜分成两份,每一份都是偶数的重量,等不等分都可以,问他们是否能成功。

陷阱:考虑w=2的情况。

源代码:

#include<iostream>using namespace std;int main(){int n;cin>>n;if(n==2)cout<<"NO";else if(n%2==0)cout<<"YES"<<endl;else cout<<"NO"<<endl;return 0;}

第二个问题:

B. 《Before an Exam》

Tomorrow Peter has a Biology exam. He does not like this subject much, butddays 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 thanminTimeiand not more thanmaxTimeihours per eachi-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 hourssumTimespent him on preparation, and now he wants to know if he can show his parents a timetablesсhedulewithdnumbers, where each numbersсheduleistands for the time in hours spent by Peter eachi-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of allscheduleishould equal tosumTime.

Input

The first input line contains two integer numbersd, 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 followingdlines contains two integer numbersminTimei, maxTimei(0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in thei-th day.

Output

In the first line printYES, and in the second line printdnumbers (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 printNOin the unique line. If there are many solutions, print any of them.

Sample test(s) input

1 48

5 7

output

NO

input

2 5

0 1

3 5

output

YES

1 4

源代码:

#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;}

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

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

思路:知道每天的最小时间和最大时间,那么 如果最小时间总和大于Sum,或者最大时间总和小于Sum,那么不能达到要求。如果Sum在最小值总和 与最大值总和之间,那么符合;

我输出的一种情况是:先比较最小值总和与Sum的大小关系,如果相等,那么输出即可,如果不等,那么从前至后依次补充时间数,直到满足为止,注意,最后补充的那一天要特殊处理;而后输出即可;

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