22分,2AC4WA3TLE,求助C++

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

yzsy25071621 @ 2023-03-01 20:39:43

我这个除了超时外应该没错吧,怎么还有4WA呢

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

bool isPrime(int);
bool isSame(int);
int main()
{
    int a=0,b=0;
    cin>>a>>b;
    int mod=0;
    for(int i=a;i<=b;i+=2)
    {
        if(isPrime(i) && isSame(i))
        {
            cout<<i<<endl;
        }
    }
    return 0;
}

bool isPrime(int m)
{
    for(int i=2;i<=floor(sqrt(m));i++)
    {
        if(m%i==0)
        {
            return false;
        }
    }
    return true;
}

bool isSame(int m)
{
    string s1="",s2="";
    s1=s2=to_string(m);
    reverse(begin(s1),end(s1));
    return s1==s2;
}

by yzsy25071621 @ 2023-03-01 21:19:58

@ZM____ML 而且我的isPrime()比isSame()要快,&&是短路运算符,换过来后反而会超时吧


by ZM____ML @ 2023-03-01 21:25:09

@yzsy25071621 isp是\sqrt n,iss是n

reverse()是O(n)


by yzsy25071621 @ 2023-03-01 21:27:27

新代码(优化isPrime()函数)

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

bool isPrime(int);
bool isSame(int);
int main()
{
    int a=0,b=0;
    cin>>a>>b;
    for(int i=a; i<=b; i++)
    {
        if(isPrime(i) && isSame(i))
        {
            cout<<i<<endl;
        }
    }
    return 0;
}

bool isPrime(int m)
{
    return (m%6==5 || m%6==1);
}

bool isSame(int m)
{
    string s1="",s2="";
    s1=s2=to_string(m);
    reverse(begin(s1),end(s1));
    return s1==s2;
}

by yzsy25071621 @ 2023-03-01 21:29:33

@ZM____ML https://blog.csdn.net/Q1368089323/article/details/109871337#:~:text=1%E3%80%81%E5%A4%A7%E4%BA%8E%E7%AD%89%E4%BA%8E5%E7%9A%84%E7%B4%A0%E6%95%B0%E5%BF%85%E5%AE%9A%E4%B8%BA6%E5%80%8D%E9%82%BB%E6%95%B0%E8%AF%81%E6%98%8E,6%E5%80%8D%E4%BB%A5%E5%A4%96%E7%9A%84%E6%95%B0%E5%88%86%E5%88%AB%E6%9C%89%EF%BC%9A6n%2B1%EF%BC%8C6n%2B2%EF%BC%8C6n%2B3%EF%BC%8C6n%2B4%EF%BC%8C6n%2B5%20%E5%85%B6%E4%B8%AD6n%2B2%EF%BC%8C6n%2B3%EF%BC%8C6n%2B4%E4%B8%89%E4%B8%AA%E6%95%B0%E9%83%BD%E5%8F%AF%E4%BB%A5%E5%88%86%E8%A7%A3%EF%BC%9A


by ZM____ML @ 2023-03-01 21:34:29

@yzsy25071621 可以,但不严谨(


by ZM____ML @ 2023-03-01 21:36:38

@yzsy25071621 素数不都是,满足的都是素数吧)


by yzsy25071621 @ 2023-03-01 21:38:32

@ZM____ML 说得对

#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

bool isPrime(int);
bool isSame(int);
int main()
{
    int a=0,b=0;
    cin>>a>>b;
    for(int i=a; i<=b; i++)
    {
        if(isPrime(i) && isSame(i))
        {
            cout<<i<<endl;
        }
    }
    return 0;
}

bool isPrime(int m)
{
    if(m%6!=1 ||m%6!=5)
    {
        return false;
    }
    for(int i=5; i<=floor(sqrt(m)); i+=6)
    {
        if(m%i==0 || m%(i+2)==0)
        {
            return false;
        }
    }
    return true;
}

bool isSame(int m)
{
    string s1="",s2="";
    s1=s2=to_string(m);
    reverse(begin(s1),end(s1));
    return s1==s2;
}

by yzsy25071621 @ 2023-03-01 21:40:29

woc,9WA


by ZM____ML @ 2023-03-01 21:56:44

@yzsy25071621 改出来了不(


by yzsy25071621 @ 2023-03-01 21:59:30

@ZM____ML 8A1T


上一页 | 下一页