11分,5个WA,3个TLE,求高人指点

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

wzy20110830 @ 2024-02-18 22:52:14

代码如下(有点长,麻烦大神耐心看完)```cpp

include <bits/stdc++.h>

using namespace std; bool judge_prime(int a) { for(int i=2;i<=sqrt(a);i++) if(a%i==0) return false; return true; } int num(int n) { int ws=0; while(n!=0) { ws++; n/=10; } return ws; } int main() { int m,n; cin>>m>>n; vector <int> prm; for(int i=m;i<=n;i++) if(judge_prime(i)) prm.push_back(i); for(int i=0;i<prm.size();i++) { if(num(prm[i])==1) cout<<prm[i]<<endl; else if(prm[i]==11) cout<<11<<endl; else if(num(prm[i])==3) if(prm[i]/100==prm[i]%10) cout<<prm[i]<<endl; else if(prm[i]==1111) cout<<1111<<endl; else if(num(prm[i])==5) if(prm[i]/10000==prm[i]%10&&prm[i]/1000%10==prm[i]/10%10) cout<<prm[i]<<endl; else if(prm[i]/1000000==prm[i]%10&&prm[i]/100000%10==prm[i]/10%10&&prm[i]/10000%10==prm[i]/100%10) cout<<prm[i]<<endl; } return 0; }


by wzy20110830 @ 2024-02-18 22:53:55

sorry,刚才打的有问题,现在再发一遍```cpp

include <bits/stdc++.h>

using namespace std; bool judge_prime(int a) { for(int i=2;i<=sqrt(a);i++) if(a%i==0) return false; return true; } int num(int n) { int ws=0; while(n!=0) {ws++;n/=10;} return ws; } int main() { int m,n; cin>>m>>n; vector <int> prm; for(int i=m;i<=n;i++) if(judge_prime(i)) prm.push_back(i); for(int i=0;i<prm.size();i++) { if(num(prm[i])==1) cout<<prm[i]<<endl; else if(prm[i]==11) cout<<11<<endl; else if(num(prm[i])==3) if(prm[i]/100==prm[i]%10) cout<<prm[i]<<endl; else if(prm[i]==1111) cout<<1111<<endl; else if(num(prm[i])==5) if(prm[i]/10000==prm[i]%10 &&prm[i]/1000%10==prm[i]/10%10) cout<<prm[i]<<endl; else if(prm[i]/1000000==prm[i]%10 &&prm[i]/100000%10==prm[i]/10%10 &&prm[i]/10000%10==prm[i]/100%10) cout<<prm[i]<<endl; } return 0; }


by __er @ 2024-02-18 23:02:54

#include <bits/stdc++.h>
using namespace std;
bool judge_prime(int a) {
    for (int i = 2; i <= sqrt(a); i++) if (a % i == 0) return false;
    return true;
}
int num(int n) {
    int ws = 0;
    while (n != 0) {
        ws++;
        n /= 10;
    } return ws;
}
int main() {
    int m, n;
    cin >> m >> n;
    vector <int> prm;
    for (int i = m; i <= n; i++) if (judge_prime(i)) prm.push_back(i);
    for (int i = 0; i < prm.size(); i++) {
        if (num(prm[i]) == 1) cout << prm[i] << endl;
        else if (prm[i] == 11) cout << 11 << endl;
        else if (num(prm[i]) == 3) if (prm[i] / 100 == prm[i] % 10) cout << prm[i] << endl;
            else if (prm[i] == 1111) cout << 1111 << endl;
            else if (num(prm[i]) == 5) if (prm[i] / 10000 == prm[i] % 10 && prm[i] / 1000 % 10 == prm[i] / 10 % 10) cout << prm[i] << endl;
                else if (prm[i] / 1000000 == prm[i] % 10 && prm[i] / 100000 % 10 == prm[i] / 10 % 10 && prm[i] / 10000 % 10 == prm[i] / 100 % 10) cout << prm[i] << endl;
    } return 0;
}

|