我尝试了许多不同的方法,但都没有成功,下面我将介绍最后一种方法:
#include<iostream>
#include<fstream>
//functions here
using namespace std;
int main(){
    int n;
    long long A[300001];
    ifstream myfile("sort.in");
    myfile>>n;
    
    for(int i=0;i<n;++i){
        myfile>>A[i];
    }
    cout<<A[0];
    myfile.close();
    
    return 0;
}这不管用。
然而,下面的方法是可行的-
#include<iostream>
#include<fstream>
//functions here
using namespace std;
int main(){
    int n;
    int A[300001];
    ifstream myfile("sort.in");
    myfile>>n;
    
    for(int i=0;i<n;++i){
        myfile>>A[i];
    }
    cout<<A[0];
    myfile.close();
    
    return 0;
}有什么问题吗?
唯一的区别是使用int而不是long long。这怎么会有这么大的区别呢?顺便说一下,不工作的意思是它不会在屏幕上产生任何输出。
发布于 2020-06-26 02:02:22
它很可能是一个stack overflow problem caused by large statically defined arrays。
而不是
long long A[300001];使用
std::vector<long long> A(300001);发布于 2020-06-26 02:06:42
在这种情况下,使用long long数组可能会导致堆栈溢出。
尝试动态分配缓冲区:
#include<iostream>
#include<fstream>
//functions here
using namespace std;
int main(){
    int n;
    
    ifstream myfile("sort.in");
    myfile>>n;
    long long *A = new long long[n];
    for(int i=0;i<n;++i){
        myfile>>A[i];
    }
    cout<<A[0];
    myfile.close();
    delete []A;
    
    return 0;
}https://stackoverflow.com/questions/62581472
复制相似问题