gpt4 book ai didi

c++ - 从不传递 long long 作为参数?

转载 作者:行者123 更新时间:2023-11-30 03:18:17 25 4
gpt4 key购买 nike

编程题是:

given a grid of numbers, how many distinct primes can you find embedded in the matrix, regarding that you can read the lines or part of them, in form vertical, horizontal or diagonal orientation, in both directions?

大编辑:对于阅读本文的任何可怜的灵魂,我通过将 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;
}

最佳答案

程序在以下部分导致未定义的行为:

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

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

long long 不能包含超过 19 位十进制数字。如果目的是验证最多 40 位的素数,那么您将不得不切换到不同的方法。

关于c++ - 从不传递 long long 作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com