44分,5个超时

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

lrqSB @ 2024-08-21 09:52:13


#include<bits/stdc++.h>
using namespace std;
int main() {
    int b=0, c = 0, v = 0, f,m;
    int a[10];
    int min, max, h=0;
    cin >> min >> max;
    for (int i = min; i <= max; i++) {
        for (int j = 1; j <= sqrt(i); j++) {
            if (i % j == 0) {
                h++;
            }
        }
        if (i <= 9) {
            f = 1;
        }
        if (i > 9 && i <= 99) {
            f = 2;
        }
        if (i > 99 && i <= 999) {
            f = 3;
        }
        if (i > 999 && i <= 9999) {
            f = 4;
        }
        if (i > 9999 && i <= 99999) {
            f = 5;
        }
        if (i > 99999 && i <= 999999) {
            f = 6;
        }
        if (i > 999999 && i <= 9999999) {
            f = 7;
        }
        if (i > 9999999 && i <= 99999999) {
            f = 8;
        }
        if (i > 99999999 && i <= 999999999) {
            f = 9;
        }
        for(int u=0;u<f;u++){
                    m=(pow(10,u));
                    a[u]=(i/m) %(10);
                }
        for (int k = 1; k <= f; k++) {
            if (a[k - 1] == a[f - k]) {
                b++;
            }
        }

        if (h == 1 && b == f ) {
            cout << i << endl;
        }
        h=0;
        b=0;
    }
}

by Emil_ @ 2024-08-21 11:04:23

@lion0617

没事了


by godogod @ 2024-08-23 14:51:24

@lion0617


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

int zs( int a ) {
    if ( a % 2 == 0 ) return 0;
    for ( int i = 3; i <= sqrt(a); i += 2 ) {
        if ( a % i == 0 ) return 0;
    }
    return 1;
}

int hw( int a ) {
    int b[11] = {0}, n = 1;
    while ( a > 0 ) {
        b[n] = a % 10;
        if ( n == 1 && b[n] == 2 && b[n] == 4 && b[n] == 6 && b[n] == 8 ) return 0; 
        a /= 10;
        n++;
    }
    n--;
    for ( int i = 1; i <= n / 2; i++ ) {
        if ( b[i] != b[n - i + 1] ) {
            return 0;
        }
    }
    return 1;
}

int main() {
    int m = 0, n = 0;
    cin >> m >> n;
    for ( int i = m; i <= n; i++ ) {
        if ( hw(i) ) {
            if ( zs(i) ){
                cout << i << endl;
            }
        }
    }
    return 0;
}

上一页 |