6 条题解

  • 5
    @ 2023-8-1 11:12:47
    #include <bits/stdc++.h>
    using namespace std;
    int ans;
    bool prime(int x)
    {
        for(int i=2;i<=sqrt(x);i++)
            if (x%i==0)
                return false;
        return true;
    }
    int main(){
        int n;
        scanf("%d",&n);
        for(int i=2;i<=n;i++)
            if(prime(i)&&i%10==7)
                ans++;
        printf("%d\n",ans);
        return 0;
    }
    
    • 3
      @ 2024-1-25 11:13:58

      数据好水,我O(nn)O(n\sqrt{n})的暴力竟然过了,还没有TLETLE

      #include <bits/stdc++.h>
      #define ll long long
      #define re register int
      using namespace std;
      int n, ans;
      bool isprime(int n)
      {
          if (n == 2)
              return true;
          for (re i = 2; i <= sqrt(n); i++)
              if (n % i == 0)
                  return false;
          return true;
      }
      int main()
      {
          ios::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          cin >> n;
          for (re i = 2; i <= n; i++)
          {
              if (isprime(i) && i % 10 == 7)
                  ans++;
          }
          cout << ans << endl;
          return 0;
      }
      
      • 0
        @ 2023-5-22 15:04:51

        Python3暴力枚举:

        1. 分析题意: 条件:素数并且个位数为7 输出:计数1-n之间满足条件的个数
        2. 解法: 只判断个位数为7的数是不是素数 素数判断条件只遍历到平方跟-1就结束
        3. 素数判断参考链接:https://blog.csdn.net/hacker_code/article/details/114038618
        import math
        n=int(input())
        sum=0
        if n<7:
            print(0)
        else:
            #只判断1-n之间个位数为7的数是不是素数
            for beichushu in range(7,n+1,10):
            #只遍历到平方根就结束,不用遍历全部
                for chushu in range(2,int(math.sqrt(beichushu))):
                    if beichushu % chushu == 0:
                        break
                else:
                    sum += 1
            print(sum)
        
        • 0
          @ 2023-2-26 15:07:50

          题目数据范围很大,普通的双重for循环根本承受不住,可以搞一个自定义函数,自定义函数框架如下:

          #include<bits/stdc++.h>
          using namespace std;
          ....   .......()//类型  函数名
          {
              .........//内容
          }
          int main()
          {
               .......
          }
          

          拓展:

          ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
          

          可以实现快读。 AC代码:

          #include<bits/stdc++.h>
          using namespace std;
          bool zhishu(int n){
              if(n<2)
              {
                  return 0;
              }
              for(int i=2;i<=sqrt(n);i++)
              {
                  if(n%i==0)
                  {
                       return 0;
                  }
              }
              return 1;
          }
          int main()
          {
              ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
              int n,num=0;
              cin>>n;
              for(int i=1;i<=n;i++)
              {
                 if(i%10==7&&zhishu(i))
                 {
                      num++;
                 }
              }
              cout<<num;
          }
          
          • 0
            @ 2022-8-6 22:06:34

            题目数据范围很大,普通的双重for循环根本承受不住,这里需要用到高级点的素数筛法
            我使用的是线性欧拉筛,时间复杂度为O(n);
            原理是把一个合数的所有倍数都标记成合数,并且确保不会重复标记。
            pri数组用来存储素数列表。
            vis数组用来存储一个数是不是素数。比如17,判断时vis[17]==0。数值为0的则为没被标记过,也就是素数。

            #include<bits/stdc++.h>
            using namespace std;
            const int maxn = 1e6+5;
            int n,cnt,pri[maxn];
            bool vis[maxn];
            void Euler(){
                for(int i=2;i<=maxn;i++){
                    if(!vis[i])pri[cnt++]=i;
                    for(int j=0;j<cnt;j++){
                        if(1ll*i*pri[j]>=maxn)break;
                        vis[i*pri[j]]=1;
                        if(i%pri[j]==0)break;
                    }
                }
            }
            int main(){
                ios::sync_with_stdio(false);
                cin.tie(0);
                cout.tie(0);
                Euler();
                cin>>n;
                cnt=0;
                for(int i=7;i<=n;i+=10){ // 只判断个位是7的数
                    if(!vis[i])cnt++; // 判断是否为素数
                }
                cout<<cnt;
                return 0;
            }
            
          • -1
            @ 2022-11-15 22:09:26

            同志们,暴力枚举不是不行,但是要取巧!(AC)

            暴力枚举(取巧)

            int meiju(int i)
            {
                if (i%6 == 1 || i%6 == 5)
                {
                //大于3的质数可以表示为6k±1!
                    for (int p=3;p*p<i;p+=2)
                    {
                        if (i%p == 0)
                        {
                            return 0;
                        }
            
                    }
                    return 1;
                }
                return 0;//大可不必加上这一行!
            }//函数体
            
            {
            cin>>a;
            for (int i=7;i<=a;i+=10)
            #个位是7的数可表示为10k+7
            {
            if meiju(i) == 1)
            {
            num++;
            }
            }
            cout<<num;
            

            思路

            一一枚举,再判断。最大时长在60ms以下。 Loading:17/100……

            • 1

            信息

            ID
            1571
            时间
            1000ms
            内存
            256MiB
            难度
            8
            标签
            递交数
            340
            已通过
            65
            上传者