8 条题解
-
10
思路:
先研究下题目:
通过题目,我们可以得知一个信息:
搬糖果的奖励的糖果数量 = k - n * 任意一个数
再研究下题目:
就可以发现:任意一个数 = k / n(k和n都是整型数)
整合一下,可以得到:
搬糖果的奖励的糖果数量 = k - n * (k / n)
知道这些以后,开始上手写代码:
先写一个for循环(循环变量为i)遍历l到r之间所有的数字
再按照上面的公式,计算当搬回来i颗时,可以得到多少颗糖果作为奖励
然后,将算出来的数量与当前最大值作比较,如果比最大值大,说明搬回i颗糖时,能拿到的作为奖励的糖目前最大,那么就将最大值更新
最后,输出最大值
注意:最大值一开始要设为0,写代码的时候能写多简单就写多简单,不然会TLE
整合亿下,就是下面的代码
#include <iostream> using namespace std; int maxn = 0; int main() { int n,l,r; cin >> n >> l >> r; for(int i = l;i <= r;i++) { int x = i / n,sum = i - n * x; maxn = (sum > maxn) ? sum:maxn; } cout << maxn; return 0; }
-
8
这题主要考察数学周期思想
若 r-l>=n 那么就一定包含了奖励为0至奖励为n-1的所有情况,其中一定包含最大的 n-1;
若 r%n<l%n 那么就一定跨越了两个周期,包含了最大值 n-1;
否则,因为l与r在同一个周期中,且 r>l,所以最大值为 r%n;
千万不要瞎暴力,否则就会TLE
#include <iostream> using namespace std; int r=1,l=1,n=1; // 初始化为1,防止RE int main() { cin >> n >> l >> r; if((r-l >= n)||(r%n<l%n)) { cout << n-1; } else { cout << r%n; } return 0; }
- 1
信息
- ID
- 1354
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 1525
- 已通过
- 316
- 上传者