洛谷P4956 [COCI2017-2018#6] Davor
[COCI2017-2018#6] Davor
题面翻译
在征服南极之后,Davor 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。他将在 2018 年 12月 31 日开始出发,在这之前需要一共筹集 n 元钱。他打算在每个星期一筹集 x 元,星期二筹集 x+k 元,……,星期日筹集 x+6k元,并连续筹集 52 个星期。其中 x,k为正整数,并且满足 1<=x<=100
现在请你帮忙计算 x,k 为多少时,能刚好筹集 n 元。
如果有多个答案,输出 x 尽可能大,k 尽可能小的。注意 k 必须大于 0。
题目描述
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 ).
样例 #1
样例输入 #1
1456
样例输出 #1
1 1
样例 #2
样例输入 #2
6188
样例输出 #2
14 1
样例 #3
样例输入 #3
40404
样例输出 #3
99 4
一星期可以获得的金币数量为7x+21k,我们可以直接枚举
观察x的取值范围,1-100。既然要x尽可能大,k的值尽可能小,那么我们可以直接从x=100开始枚举,k的值从1枚举到1000(观察到结果最大也就145600)如果算得的(7x+21k)*52刚好等于输入的值,
那么此时的x,k就是答案:
C++代码如下:
#include<iostream> using namespace std; int main() { int n; cin >> n; for (int i = 100; i >= 1; i--) { for (int j = 1; j <= 1000; j++) { int sum = 7 * i + 21 * j; sum *= 52; if (sum == n) { cout << i << endl << j; return 0; } } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战