8超时,实在没办法
查看原帖
8超时,实在没办法
922134
jiejielove楼主2023/2/25 17:33
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<string>
#include<algorithm>
#include <cstdlib>
#include<stdlib.h>
#include<sstream>
using namespace std;

int main()
{
    int m, n;
    int p, q;
    int count=0;
    string s;
    int c;
    cin >> m >> n;
    for (int i = m; i<= n; i++)
    {
        c = i;
        p = 0;
        q = 0;
        for (int j = 2; j<i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            if (j == m - 1)
            {
                p = 1;
            }
        }
        s = to_string(c);
        reverse(s.begin(), s.end());
        c = atoi(s.c_str());
        for (int j = 2; j<c; j++)
        {
            if (c % j == 0)
            {
                break;
            }
            if (j == c - 1)
            {
                q = 1;
            }
        }
        if (p == 1 && q == 1)
        {
            count++;
        }
    }


    if (count == 0)
    {
        cout << "No";
        return 0;
    }


    for (int i = m; i <= n; i++)
    {
        c = i;
        p = 0;
        q = 0;
        for (int j = 2; j<i; j++)
        {
            if (i % j == 0)
            {
                break;
            }
            if (j == m - 1)
            {
                p = 1;
            }
        }
        s = to_string(c);
        reverse(s.begin(), s.end());
        c = atoi(s.c_str());
        for (int j = 2; j<c; j++)
        {
            if (c % j == 0)
            {
                break;
            }
            if (j == c - 1)
            {
                q = 1;
            }
        }
        if (p == 1 && q == 1)
        {
            if (count != 1)
            {
                cout << i << ",";
            }
            else
            {
                cout << i;
            }
            count--;
        }
    }

    return 0;
}
2023/2/25 17:33
加载中...