【菜鸟进阶之路】P4956 [COCI2017-2018#6] Davor - 洛谷

一、题目部分

题目描述
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 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。他将在 20182018 年 1212 月 3131 日开始出发,在这之前需要一共筹集n元钱。他打算在每个星期一筹集x元,星期二筹集x+k 元,……,星期日筹集x+6k元,并在 52个星期内筹集完。其中x,k为正整数,并且满足1≤x≤100。

现在请你帮忙计算x,k为多少时,能刚好筹集 n元。

如果有多个答案,输出 x 尽可能大,k尽可能小的。注意 k 必须大于 0。

输入输出样例

输入 #1
1456
输出 #1
1
1

输入 #2
6188
输出 #2
14
1

输入 #3
40404
输出 #3
99
4

二、解题过程

思路
(1)输入总金额数n;
(2)算出一周内需要筹集的金额数;
(3)根据题目推算出x、k和一周内金额数的关系,对关系公式进行约分;
(4)从1开始,对k进行循环赋值,根据公式求出x,若满足1≤x≤100,输出k、x,否则继续循环直到满足题目要求。

提交AC答案

#include
using namespace std;
int n,wk;//筹集总金额数n,一周金额数wk
int x,k;//最后输出的两个变量 

int main()
{	
	scanf("%d",&n);
	
	wk=n/52;// wk=7x+21k 
	
	wk=wk/7;//根据公式约分 wk=x+3k 
	
//	printf("%d",wk); 
	
	for(int i=1;;i++)
	{
		k=i;
		x=wk-3*k;
		if(x>=1&&x<=100)
		{
			printf("%d\n%d",x,k);
			break;
		}
		else
		{
			k++;
		}
	}
	
//	printf("%d\n%d",x,k);
	
	return 0;
}

三、小结
(1)重点理解“输出 x 尽可能大,k尽可能小”这句话;
(2)有空总结一下for循环的一些特殊用法。

你可能感兴趣的:(算法学习)