P1217 为什么55分?

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

luogujisuanji @ 2024-06-05 13:32:23

#include<bits/stdc++.h>
using namespace std;
int hw(int num){
    int h=0,n=num;
    while(n!=0){
        h=h*10+n%10;
        n/=10;
    }
    if(h==num)return 1;
    return 0;
}
int prime(int num){
    if(num==1||num%2==0)return 0;
    else for(int i=3;i<sqrt(num);i++)if(num%i==0)return 0;
    return 1;
}
int main(){
    int a,b;
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        if(i==9989900)break;
        if(hw(i)&&prime(i))cout<<i<<endl;
    }
    return 0;
}

by King_and_Grey @ 2024-06-05 14:39:34

@luogujisuanji

判断素数

int prime(int num){
    if(num==1||num%2==0)return 0;
    else for(int i=3;i<sqrt(num);i++)if(num%i==0)return 0;
    return 1;
}

应该为

bool prime(int num){
    if(num==1)return 0;
    else for(int i=2;i<sqrt(num);i++)if(num%i==0)return 0;
    return 1;
}

by luogujisuanji @ 2024-06-06 20:29:46

@greyandking 栓q,已关


by luogujisuanji @ 2024-06-06 20:31:41

但改了之后还是55分啊


by King_and_Grey @ 2024-06-06 21:06:48

@luogujisuanji 等我一下,我把我的代码给你看(等我先打一下)


by luogujisuanji @ 2024-06-06 21:09:33

好的


by King_and_Grey @ 2024-06-06 21:11:54

@luogujisuanji

#include <bits/stdc++.h>
using namespace std;

int a,b,ans;
string s,c;
bool isPrime(int k){
    if(k == 1){
        return false;
    }
    for(int i = 2;i <= sqrt(k);i++){
        if(k % i == 0){
            return false;
        }
    }
    return true;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> a >> b;
    for(int i = a;i <= b;i++){
        int l = i;
        while(l != 0){
            s += to_string(l % 10);
            l /= 10;
        }
        reverse(s.begin(),s.end());
        c = s;
        reverse(s.begin(),s.end());
        if(s == c && isPrime(i) == true){
            cout << i << "\n";
        }
        s = "";
    }
    return 0;
}

后面几个TLE,我先给你看一下,马上改


by luogujisuanji @ 2024-06-06 21:15:24

@greyandking Ok


by King_and_Grey @ 2024-06-06 21:15:38

@luogujisuanji 这一题马上用的方法要证明,再深入浅出上有(你有深入浅出吗?)


by luogujisuanji @ 2024-06-06 21:18:42

@greyandking 没有。。。


by luogujisuanji @ 2024-06-06 21:19:31

但是我大概能看明白这个意思


| 下一页