4.4 课后习题与实验(5)
习题 4-10
Davor(洛谷P4956,COCI2017)
After successfully conquering the South Pole, Davor is preparing for new challenges. Next up is the Arctic expedition to Siberia, Greenland and Norway. He begins his travels on 31 December 2018, and needs to collect N kunas (Croatian currency) by then. In order to do this, he has decided to put away X (X ≤ 100) kunas every Monday to his travel fund, X + K kunas every Tuesday, X + 2* K every Wednesday, and so on until Sunday, when he will put away X + 6* K kunas. This way, he will collect money for 52 weeks, starting with 1 January 2018 (Monday) until 30 December 2018 (Sunday).
If we know the amount of money N, output the values X and K so that it is possible to collect the exact money amount in the given timespan. The solution will always exist, and if there are multiple, output the one with the greatest X and smallest K .
输入格式
The first line of input contains the integer N (1456 ≤ N ≤ 145600), the number from the task.
输出格式
The first line of output must contain the value of X (0 < X ≤ 100 ), and the second the value of K (K > 0 ).
题意翻译
在征服南极之后,Davor 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。他将在 2018 年 12 月 31 日开始出发,在这之前需要一共筹集 n 元钱。他打算在每个星期一筹集 x 元,星期二筹集 x+k 元,……,星期日筹集 x+6k 元,并在 52 个星期内筹集完。其中 x,k 为正整数,并且满足 1≤x≤100。
现在请你帮忙计算 x,k 为多少时,能刚好筹集 n 元。
如果有多个答案,输出 x 尽可能大,k 尽可能小的。注意 k 必须大于 0。
输入输出样例
| 输入 | 输出 |
|---|---|
1456 |
11 |
6188 |
141 |
40404 |
994 |
解答
#include<iostream>
using namespace std;
int main()
{
int n,x;
cin>>n;
//x x+k x+2k x+3k x+4k x+5k x+6k
//(7x+21k)*52=n
//x+3k=n/52/7
n=n/52/7;
for(int k=1;k<n;k++)
{
x=n-3*k;
if(x<=100)
{
cout<<x<<endl<<k;
break;
}
}
return 0;
}
习题 4-11
津津的储蓄计划(洛谷P1089,NOIP2004提高组)
津津的零花钱一直都是自己管理。每个月的月初妈妈给津津300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同。
为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里,到了年末她会加上20%还给津津。因此津津制定了一个储蓄计划:每个月的月初,在得到妈妈给的零花钱后,如果她预计到这个月的月末手中还会有多 100元或恰好100元,她就会把整百的钱存在妈妈那里,剩余的钱留在自己手中。
例如11月初津津手中还有83元,妈妈给了津津300元。津津预计11月的花销是180元,那么她就会在妈妈那里存200元,自己留下183元。到了11月月末,津津手中会剩下3元钱。
津津发现这个储蓄计划的主要风险是,存在妈妈那里的钱在年末之前不能取出。有可能在某个月的月初,津津手中的钱加上这个月妈妈给的钱,不够这个月的原定预算。如果出现这种情况,津津将不得不在这个月省吃俭用,压缩预算。
现在请你根据2004年1月到12月每个月津津的预算,判断会不会出现这种情况。如果不会,计算到2004年年末,妈妈将津津平常存的钱加上20%还给津津之后,津津手中会有多少钱。
输入格式
12行数据,每行包含一个小于350的非负整数,分别表示1月到12月津津的预算。
输出格式
一个整数。如果储蓄计划实施过程中出现某个月钱不够用的情况,输出−X,X表示出现这种情况的第一个月;否则输出到2004年年末津津手中会有多少钱。
注意,洛谷不需要进行文件输入输出,而是标准输入输出。
输入输出样例
| 输入 | 输出 |
|---|---|
29023028020030017034050 90 80 20060 |
-7 |
290 230 280 200 300 170 330 50 90 80 200 60 |
1580 |
解答
#include<iostream>
using namespace std;
int main()
{
int n,s=0,z=0,flag=1;
for(int i=1;i<=12;i++)
{
cin>>n;
s+=300;
s-=n;
if(s<0)
{
cout<<'-'<<i;
flag=0;
break;
}
if(s>100)
{
z+=s-s%100;
s%=100;
}
}
if(flag) cout<<z*12/10+s;
return 0;
}

浙公网安备 33010602011771号