永远不要长时间作为参数?

问题描述 投票:1回答:1

编程问题是:

给定一个数字网格,你可以在矩阵中找到多少个不同的素数,关于你可以在两个方向上以垂直,水平或对角线的形式读取它们的线条或部分线条?

大编辑:对于读这个的任何可怜的灵魂,我通过在我的功能下添加if (n > INT_MAX) return false;到第一行来解决这个问题!

只要isPrime的参数不是const ll &n,我的程序就不会崩溃,我想知道为什么。是因为你永远不应该将long long作为参数传递,因为它太大了?或者是因为如果你将int添加到long long整数,它不再变成long long

我用来测试我的工作的输入是:

40
0251677085866837460317708637021446063144
8812262220360202463050064531874436437682
5251855367278508848642345043775871434078
0042675865438283025822603307175060748288
5672321632434878440388701468545837465571
3448326728143606881852187616524878044060
8876415778774852362710315274652021065556
1406474838287088561242126854006826771778
7827443331184330371521043472218803550383
6318874838447276075161123302780187880165
0884752758538865306583258450386821283658
1260362124615176735303563717773657467333
2580363145201308707655341168610513145546
4142635386876777348215436708254351251288
5301330463217260626047132625527161775404
8620446353006857360714856156584322276288
0813375760405334773480860674772272733638
6715558007108501053612008324501255710425
8840634327383685827335506853781648727036
8827728873376824454871536655067801443735
0664640563836487481174816586572628815173
7186752536147276768154002317573417465332
4438770023402783205544064640821537621225
4162442401558771474140203865162080237721
5008757506737224070577338578644664641338
2155803687408638660278862273674652462840
2118148017744113203720114756276821067158
4838003412436782114402742024145245437315
5161343527676283186170466281455700086618
7723886261287175705152273086317588317188
6653360024271146551000054710768617586846
0050014847531086708661266564560614115164
3351156208161708784441387827072734346251
0457546342466313073230326436563643506534
3837451141488371231210888733717540046582
3334248265835234158638343058444640886465
0173240266426385002380821305357684721128
0437020214873361352055818843664073456138
3858604586068245520287541000014334760058
5840781588142205318614583635575571714673

我的代码在这里:

#include <iostream>
#include <set>
#include <vector>

typedef long long ll;

const int DIRECTIONS[][2] = {{1, 0}, {0, 1}, {1, 1}, {-1, 0}, {0, -1}, {-1, -1}, {1, -1}, {-1, 1}};

bool isPrime(const int &n) {
        if (n != 2) {
                if (n < 2 || !(n & 1)) return false;
                for (ll i = 3; i * i <= n + 1; i += 2) {
                        if (n % i == 0) return false;
                }
        }
        return true;
}

int main() {
        int length = 0;
        std::set<ll> primes{};
        char c;
        std::cin >> length;
        std::vector<std::vector<ll>> grid;
        for (int i = 0; i < length; i++) {
                std::vector<ll> row;
                for (int j = 0; j < length; j++) {
                        std::cin >> c;
                        row.push_back(c - '0');
                }
                grid.push_back(row);
        }
        for (int i = 0; i < length; i++) {
                for (int j = 0; j < length; j++) {
                        for (auto d : DIRECTIONS) {
                                int tempi = i, tempj = j;
                                ll base10 = 0;
                                while (i < length && j < length && i >= 0 && j >= 0) {
                                        base10 *= 10;
                                        base10 += grid[i][j];
                                        if (isPrime(base10)) primes.insert(base10);
                                        i += d[0];
                                        j += d[1];
                                }
                                i = tempi;
                                j = tempj;
                        }
                }
        }
        std::cout << "Number of primes: " << primes.size() << std::endl;
        /*std::cout << "List of primes: " << std::flush;
        for (auto prime : primes) {
                std::cout << prime << " " << std::flush;
        }*/
        return 0;
}
c++ long-integer
1个回答
2
投票

该程序在部件中导致未定义的行为:

base10 *= 10;
base10 += grid[i][j]

因为这些计算有时会超过long long的最大值。

long long可能不会超过19位小数。如果目的是验证最多40位数的素数,那么您将不得不切换到不同的方法。

© www.soinside.com 2019 - 2024. All rights reserved.