Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 永远不要像参数一样长时间传递?_C++_Long Integer - Fatal编程技术网

C++ 永远不要像参数一样长时间传递?

C++ 永远不要像参数一样长时间传递?,c++,long-integer,C++,Long Integer,编程问题是: 给定一个数字网格,你能在矩阵中找到多少不同的素数,关于你能在两个方向上以垂直、水平或对角线方向读取线或线的一部分 大编辑:对于任何阅读本文的可怜的灵魂,我通过添加if(n>INT\u MAX)return false来解决这个问题到我的函数下的第一行 只要isPrime的参数不是const ll&n,我的程序就不会崩溃,我想知道为什么。是不是因为它太大,所以永远不应该将long作为参数传递?或者是因为如果将int添加到long整数,它就不再是long 我用来测试我的工作的输入是:

编程问题是:

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

大编辑:对于任何阅读本文的可怜的灵魂,我通过添加
if(n>INT\u MAX)return false来解决这个问题到我的函数下的第一行

只要
isPrime
的参数不是
const ll&n
,我的程序就不会崩溃,我想知道为什么。是不是因为它太大,所以永远不应该将
long
作为参数传递?或者是因为如果将
int
添加到
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;
}
#包括
#包括
#包括
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(常量整数和整数){
如果(n!=2){
如果(n<2 | |!(n&1))返回false;
对于(ll i=3;i*i>长度;
矢量网格;
for(int i=0;i>c;
行。向后推(c-‘0’);
}
网格。向后推(世界其他地区);
}
for(int i=0;i=0&&j>=0){
基数10*=10;
base10+=网格[i][j];
if(isPrime(base10))素数插入(base10);
i+=d[0];
j+=d[1];
}
i=tempi;
j=tempj;
}
}
}

std::cout程序会导致零件出现未定义的行为,包括:

base10 *= 10;
base10 += grid[i][j]
因为这些计算有时超过了
long
的最大值


long-long
的小数位数可能不超过19位。如果要验证最多40位的素数,则必须切换到其他方法。

int grid[length][length];
在标准C++@M.M中不允许它在代码块中正确编译?它不应该编译吗?它依赖于非标准extensions@BoWork想想如果你提供1000000000作为长度,会发生什么。这是一个非常大的自动存储空间消耗。打赌你没有超过10000000字节。你需要一个big文本文件,如果您打算以这种方式验证40位素数