8 条题解

  • 16
    @ 2023-9-14 20:49:31

    不会吧,这么简单 应该都会吧......(先 后看!养成习惯!)

    #include<bits/stdc++.h>
    using namespace std;
    int n,l,r;
    int main()
    {
    	cin>>n>>l>>r;
    	if(l/n==r/n)
        {
            cout<<r%n;
        }
    	else
        {
            cout<<n-1;
        }
    	return 0;
    }
    

    制作不易,给个赞吧,球球了...... 有什么问题,联系我,邮箱是ASheepBoy_Bed@163.com image

    • 10
      @ 2023-7-18 22:50:05

      思路:

      先研究下题目:

      通过题目,我们可以得知一个信息:

      搬糖果的奖励的糖果数量 = 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
        @ 2023-9-3 18:48:11

        这题主要考察数学周期思想

        若 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;
        }
        
        • @ 2023-10-16 22:47:26

          很赞同你的思路,不过我试过了暴力,也没TLE……

      • 6
        @ 2023-9-24 10:49:14

        这题很简单。难独居然是

        8???

        #include <bits/stdc++.h>
        using namespace std;
        long long n,l,r;
        int main(){
            cin>>n>>l>>r;
            if((l/n+1)*n<=r)
                cout<<n-1;
            else
                cout<<r%n;
            return 0;
        }
        

        实在不知道该咋写注释,自己理解吧。

        • 4
          @ 2024-3-12 20:43:45
          #include <bits/stdc++.h>
          using namespace std;
          int main()
          {
              int n,l,r;
              cin>>n>>l>>r;
              if((l/n+1)*n<=r)
                  cout<<n-1;
              else
              {
                  cout<<r%n;
              }
              return 0;
          }
          
        • 4
          @ 2024-2-4 11:12:22

          暴力解法优化

          分析: 理解透彻这个题目后,就会发现自己的奖励就是 拿的糖果数%人数(不信自己算算)! 因此,只需要遍历L~R,求取商最大值即可。 AC代码:

          #include<iostream>
          using namespace std;
          int n,l,r,ma;//n为人数,l、r为上下限,ma为最大奖励值
          int main(){
              cin>>n>>l>>r;
              for(int i=l;i<=r;i++){
                  if(i%n>ma)  ma=i%n;//最大值更替法
              }
              cout<<ma;
              return 0;
          }
          
          • 4
            @ 2023-6-17 18:38:07
            #include<iostream>
            #include<cstdio>
            using namespace std;
            
            int n,l,r;
            
            int main(){
            	cin>>n>>l>>r;
            	if(l/n==r/n) cout<<r%n;
            	else cout<<n-1;
            	return 0;
            }
            
            • 3
              @ 2023-9-18 20:23:21
              #include <iostream>
              using namespace std;
              int r=1,l=1,n=1; 
              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
              上传者