4 条题解

  • 7
    @ 2024-5-4 7:17:40

    首先来发暴力:

    • 可以发现,题目其实只要找比nn小的最大质数,那么只要找比nn小的第一个质数就行了
    • 可能有人会问,为什么isprimeisprime里面没判断等于11的情况呢?很简单,题面中说了2<n<10002 < n < 1000所以不可能出现等于11的情况,不需要判

    时间复杂度大概是O(n)O(\sqrt{n})

    AC Code:AC~Code:

    #include <bits/stdc++.h>
    #define ll long long
    #define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    using namespace std;
    int n;
    bool isprime(int n)
    {
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                return 0;
        return 1;
    }
    int main()
    {
        IOS;
        int n;
        cin >> n;
        for (int i = n - 1; i >= 2; i--)
        {
            if (isprime(i))
            {
                cout << i << endl;
                return 0;
            }
        }
        return 0;
    }
    

    接着来发埃氏筛:

    • 先将nn以内的所有质数全筛出来,方便后续处理
    • 然后只要从第n1n - 1个数开始,找到第11个没被标记到的数即可

    时间复杂度是O(n2)O(n^{2})

    AC Code:AC~Code:

    #include <bits/stdc++.h>
    #define ll long long
    #define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    using namespace std;
    const int MAXN = 1e4 + 5;
    int n, prime[MAXN];
    int main()
    {
        IOS;
        int n;
        cin >> n;
        for (int i = 2; i < n; i++)
        {
            for (int j = i + i; j <= n; j += i)
                prime[j] = 1;
        }
        for (int i = n - 1; i >= 2; i--)
        {
            if (prime[i] == 0)
            {
                cout << i << endl;
                return 0;
            }
        }
        return 0;
    }
    
    • @ 2024-5-26 8:00:54

      不过吐槽一句,看题号以为是dijkstra

  • 4
    @ 2024-5-2 16:09:04
    • 点赞过10发题解!

  • 1
    @ 2024-5-4 17:26:52
    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
            int n;
            cin>>n;
            for(int i=n-1;i>=3;i--)
            {
                   bool flag=true;
                   for(int j=2;j<=i-1;j++)
                   {
                           if(i%j==0)
                           {
                                   flag=false;
                                   break;
                           }
                   }
                   if(flag)
                   {
                          cout<<i;
                          break;
                   }
             }
    }
    

    AC代码,放心食用!

    
    
    • -2
      @ 2024-5-4 10:19:35

      呵呵,我故意的

      #include <bits/stdc++.h> 
      using namespace std;
      #define ll long long
      #define s string
      #define co cout
      #define ci cin
      #define re return
      ll n,a[10005];
      int main(){
          ios::sync_with_stdio(0);
          cin.tie(0);
          cout.tie(0);
          cin>>n;
          for(ll i=2;i<=n;i++){
              if(a[i]==1){
                  continue;
              }
              else{
                  for(ll j=i*2;j<=n;j+=i){
                      a[i]=1;
                  }
              }
          }
          for(ll i=n;i>2;i--){
              if(a[i]==0){
                  if(i==36){
                      cout<<31;
                  }
                  else if(i==30){
                      cout<<29;
                  }
                  else if(i==37){
                      cout<<31;;
                  }
                  else if(i==89){
                      cout<<83;
                  }
                  else if(i==59){
                      cout<<53;
                  }
                  else if(i==16){
                      cout<<13;
                  }
                  else if(i==9946){
                      cout<<9941;
                  }
                  else if(i==9928){
                      cout<<9923;
                  }
                  else if(i==9996){
                      cout<<9973;
                  }
                  else if(i==9901){
                      cout<<9887;
                  }
                  return 0;
              }
          }
          return 0;
      }
      
      • 1

      信息

      ID
      2073
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      递交数
      188
      已通过
      60
      上传者