罗小白


私信TA

用户名:Timmmmy

访问量:16378

签 名:

隔一年又回来刷题了...

等  级
排  名 140
经  验 7192
参赛次数 0
文章发表 46
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

有问题可以互相交流,共同提高 欢迎私信,请多指教:)

解题思路:
        欧拉筛法打表,不知道为啥过不了,更匪夷所思的是:

        复制了另一位已通过同学 (@用户名:wangpin) 的代码,与我的结果作比较

        测试范围(2~300000)均没有发现异常 (题目约定 a, b 均属于 [2, 10000]

        测试代码在下方

        求大神指点或官方解答


注意事项:

       ! 以下代码为测试代码,勿提交平台,无法AC


参考代码:

#include<bits/stdc++.h>

using namespace std;

#define N 300000

// 我定义的函数
string minFactor(int num, bool isP[], int P[], int Pcount)
{
    string res = std::to_string(num) + "=";
    if (isP[num]) {
        res += std::to_string(num);
        return res;
    }

    for (int i = 0; i < Pcount; ++i) {
        if (num == 1) break;
        while (num % P[i] == 0) {
            res = res + std::to_string(P[i]) + "*";
            num /= P[i];
        }
    }
    if (res[res.length() - 1] == '*') {
        res = res.substr(0, res.length() - 1);
    }
    return res;
}

// 可AC的函数
string A(int n)
{
    int prime[200], indexP = 0, factor[200], indexF = 0;
    string res = "";
    for (int i = 2; i <= n; i++)    // 求小于这个数的素数 
    {                      // n前面要有等于号,因为当n为素数是质因数就是它本身 
        if (n%i == 0)
        {
            prime[indexP++] = i;
        }
    }
    while (n > 1)    //求这个数的质因数 
    {
        for (int i = 0; i < indexP; i++)
        {
            if (n%prime[i] == 0)   // 能整除的最小素数就是他的质因数 
            {
                factor[indexF++] = prime[i];
                n /= prime[i];
                break;
            }
        }
    }
    for (int i = 0; i < indexF - 1; i++)  
    {
        res += (std::to_string(factor[i]) + "*");
    }
    res += std::to_string(factor[indexF - 1]);
    return res;
}

int main()
{
    int i, j, primeCnt = 0;
    bool* isPrime = new bool[N + 5];
    int* Prime = new int[N + 5];
    memset(isPrime, true, sizeof(isPrime));
    isPrime[0] = isPrime[1] = false;
    for (i = 2; i <= N; i++) {
        if (isPrime[i]) Prime[primeCnt++] = i;
        for (j = 0; j < primeCnt && i*Prime[j] <= N; j++) {
            isPrime[i*Prime[j]] = false;    
            if (i % Prime[j] == 0) break;    
        }
    }

    int a, b;
    while (cin >> a >> b) {
        for (int i = a; i <= b; ++i) {
            // 我定义的函数 minFactor
            string s_mine = minFactor(i, isPrime, Prime, primeCnt);
             
            // 可AC的函数 A
            string s_correct = std::to_string(i) + "=";
            s_correct += A(i);

            // 如果有不同,应当输出
            if (s_mine != s_correct) {
                cout << "My:        " << s_mine << endl;
                cout << "Correct:    " << s_correct << endl;
            }
        }
    }

    delete[] isPrime, Prime;

    return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区