11pts,help

P1217 [USACO1.5] 回文质数 Prime Palindromes

jfy2204 @ 2023-06-23 12:53:24

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,s,flag=1;
    cin>>a>>b;
    if(a<=5 && b>=5) 
        cout<<5<<endl;
    if(a<=7 && b>=7)
        cout<<5<<endl;
    if(a<=11 && b>=11)
        cout<<11<<endl;
    for(int i1=1; i1<=9; i1+=2)
        for(int i2=0; i2<=9; i2++)
        {
            s=100*i1+10*i2+i1;
            if(s<a)
                continue;
            if(s>b)
                return 0;
            for(int j=3; j*j<=s; j++)
                if(s%j==0)
                {
                    flag=0;
                    break;
                }
            if(flag)
                cout<<s<<endl;
        }
    for(int i1=1; i1<=9; i1+=2)
        for(int i2=0; i2<=9; i2++)
            for(int i3=0; i3<=9; i3++)
            {
                s=10000*i1+1000*i2+100*i3+10*i2+i1;
                if(s<a)
                    continue;
                if(s>b)
                    return 0;
                for(int j=3; j*j<=s; j++)
                    if(s%j==0)
                    {
                        flag=0;
                        break;
                    }
                if(flag)
                    cout<<s<<endl;
            }
        for(int i1=1; i1<=9; i1+=2)
            for(int i2=0; i2<=9; i2++)
                for(int i3=0; i3<=9; i3++)
                    for(int i4=0; i4<=9; i4++)
                    {
                        s=1000000*i1+100000*i2+10000*i3+1000*i4+100*i3+10*i2+i1;
                        if(s<a)
                            continue;
                        if(s>b)
                            return 0;
                        for(int j=3; j*j<=s; j++)
                            if(s%j==0)
                            {
                                flag=0;
                                break;
                            }
                        if(flag)
                            cout<<s<<endl;
                    }
    return 0;
}

by Starstream @ 2023-06-26 20:53:03

建议打表

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

bool is_palindrome(int num)
{
    string s = to_string(num);
    int left = 0, right = s.length() - 1;
    while (left < right)
        if (s[left++] != s[right--])
            return false;
    return true;
}

bool is_prime(int num)
{
    if (num < 2) return false;
    int k = sqrt(num);
    for (int i = 2; i <= k; ++i)
        if (num % i == 0) return false;
    return true;
}

int main()
{
    vector<int> result;
    for (int i = 1; i <= 100000000; ++i)
        if (is_palindrome(i) && is_prime(i))
            result.push_back(i);
    for (int i : result)
        cout << i << ",";
    return 0;
}

by jfy2204 @ 2023-06-27 12:30:29

拜托,我是新手


by qwiwiiwjjdks @ 2023-07-02 14:20:17

没必要打表!!!

我代码全AC————————100ms

#include<bits/stdc++.h>
using namespace std;
bool pdzs(int n){
    if(n<2) return false;
    for(int i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}
bool h(int n){
    int y = 0,k = n;
    while(n>0){
        y = y*10+n%10;
        n = n/10;
    }
    if(y==k) return true;
    else return false;
} 
int main(){
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(i>9989899) return 0;
        else{
            if(i!=2 && i%2==0) continue;
            else if(h(i)){
            if(pdzs(i)) printf("%d\n",i);
            }
            else continue;
        }

    }
    return 0;
}

by qwiwiiwjjdks @ 2023-07-02 14:20:58

@GMX_Harry 6


by qwiwiiwjjdks @ 2023-07-02 14:22:57

@jfy2204 看我代码!!入门级


by jfy2204 @ 2023-07-03 16:02:22

虽然是入门,但是我仍然看不懂???


by qwiwiiwjjdks @ 2023-07-04 13:01:20

@qwiwiiwjjdks 'bestman_me'是我小号,希望管理员不要棕名!


by craftmine @ 2023-07-06 06:41:56

你的判断条件错了:

for(int j=3;j*j<=s;j++)
   if(s%j==0){
     flag=0;
     break;
    }

正确代码:

for(int j=2;j*j<=s;j++)
   if(s%j==0){
     flag=0;
     break;
    }

by craftmine @ 2023-07-06 06:45:23

@jfy2204 他是枚举3,5,7位的质数


by craftmine @ 2023-07-07 14:11:14

还有这个:

if(a<=7 && b>=7)
        cout<<5<<endl;

应该这样:

if(a<=7 && b>=7)
        cout<<7<<endl;

| 下一页