尝试把素数结果保存到文件:
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
const int n=1000;
int main(){
int a[n];
int i,j;
ofstream ofile;
ofile.open("d:\\myfile.txt");
for(i=0;i<n;i++) a[i]=1+i;
a[0]=0;
for(i=1;i<n;i++){
if(a[i]==0) continue;
for(j=i+1;j<n;j++) if(a[j]%a[i]==0) a[j]=0;
}
int count=0;
ofile<<"1-"<<n<<"之间的素数:"<<endl;
for(i=0;i<n;i++)
if(a[i]!=0){
ofile<<setw(6)<<a[i];
count++;
if(count%10==0) ofile<<endl;
}
ofile<<endl;
ofile.close();
return 0;
}
保存文件后,方便以后使用或查看!