题面翻译
在征服南极之后,Davor 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。他将在 2018 2018 2018 年 12 12 12 月 31 31 31 日开始出发,在这之前需要一共筹集 n n n 元钱。他打算在每个星期一筹集 x x x 元,星期二筹集 x + k x+k x+k 元,……,星期日筹集 x + 6 k x+6k x+6k 元,并连续筹集 52 52 52 个星期。其中 x , k x,k x,k 为正整数,并且满足 1 ≤ x ≤ 100 1 \le x \le 100 1≤x≤100。
现在请你帮忙计算 x , k x,k x,k 为多少时,能刚好筹集 n n n 元。
如果有多个答案,输出 x x x 尽可能大, k k k 尽可能小的。注意 k k k 必须大于 0 0 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.题目分析
该题只要考查的是循环和解方程组,这里用穷举就可以解决。文章来源:https://www.toymoban.com/news/detail-615935.html
2.题目思路
第一周筹集的资金是:(7x + 21k),那么52周就是:(7x + 21k)*52 = n,
这样就可以得到判断条件,写两个循环分别枚举K和X,得到多组解,但要求X要尽可能的小,
定义一个变量记录第一次最小值,然后判断解中的最小值,保存相应的解。打印输出即可。文章来源地址https://www.toymoban.com/news/detail-615935.html
3.代码实现
#include <stdio.h>
int main() {
int n, x, k;
scanf("%d", &n);
int min,max;
int count = 1;
//穷举
for (x = 0; x <= 100; x++) {
for (k = 1; k < 1000; k++) {
//判断方程的解
if (52 * (7 * x + 21 * k) == n){
//记录第一个解,看作最小值
if (count == 1){
min = k;
count--;
}
//判断最小值,并赋值
if (k <= min){
min =k;
max = x;
}
}
}
}
//打印结果
printf("%d\n", max);
printf("%d\n",min);
return 0;
}
到了这里,关于P4956 [COCI2017-2018#6] Davor的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!