4 条题解

  • 3
    @ 2021-10-25 20:03:12

    很简单

    梳理一下题目大意:在[l,r]\rm [l,r]这个区间,找到一个数,使得他模nn的值最大

    • 如果l/n==r/n的值相同,就选最大的r,输出r%n
    • 不同,肯定就是n-1

    其实这道题有个做法,就是先把暴力写出来,找规律

    //核心代码
    if(l/n==r/n) cout<<r%n<<endl;
    else cout<<n-1<<endl;
    

    还有一个偏暴力的不是很正规的做法:

    int maxn=-100;
    for(int i=l;i<=r;i++){
        maxn=max(maxn,i%n);//按照题意模拟
        if(maxn==n-1){//因为答案最大就是n-1,如果你现在已经是n-1了,那么就可以退出循环
            cout<<maxn<<endl;
            return 0;
        }
    }
    cout<<maxn<<endl;
    • 1
      @ 2024-2-21 21:54:26

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

      • 1
        @ 2023-9-17 22:36:19

        题还算简单,代码如下:

        #include <bits/stdc++.h>
        using namespace std;
        int n,l,r;
        int main(){
        	cin>>n>>l>>r;
            //核心if条件语句
        	if((l%n+(r-l))>=n) cout<<n-1<<endl;
        	else cout<<max(l%n,r%n)<<endl;
        }
        
        • 0
          @ 2023-10-22 14:16:26
          #include <iostream>
          using namespace std;
          int main()
          {
          	int n,L,R;
          	cin>>n>>L>>R;
          	if (L % n > R % n)
          	{
          		cout<<n-1;
          	}
          	else if (L % n == R % n)
          	{
          		if (R > L)
          		{
          			cout<<n-1;
          		}
          		else
          		{
          			cout<<R%n;
          		}
          	}
          	else
          	{
          		if (R-L>n)
          		{
          			cout<<n-1;
          		}
          		else
          		{
          			cout<<R%n;
          		}
          	}
          	return 0;
          }
          
          • 1

          信息

          ID
          188
          时间
          1000ms
          内存
          512MiB
          难度
          2
          标签
          递交数
          9074
          已通过
          5365
          上传者