Problem Description
Easy question! Calculate how many primes between [1...n]!

Input
Each line contain one integer n(1 <= n <= 1e11).Process to end of file.

Output
For each case, output the number of primes in interval [1...n]

Sample Input
2
3
10

Sample Output
1
2
4

1个回答

// 注意包含的头文件应该是用尖括号，这里只是为让其能显示
#include "stdio.h"
#include "vector"
using namespace std;

int main()
{
vector v;
unsigned long n;
unsigned long numPrime; // 素数个数
FILE * fp;

``````fp = fopen("in.txt","r");   // 打开输入文件

fscanf(fp,"%d",&n);         // 读取数据并将数据保存到vector容器中
while(!feof(fp))
{
v.push_back(n);
fscanf(fp,"%d",&n);
}
fclose(fp);                 // 关闭输入文件

fp = fopen("out.txt","w");  // 打开输出文件
for(long i = 0;i<v.size();i++)
{
fprintf(fp,"0至%d的素数：",v[i]);
numPrime = 0;
for (long j = 2; j <= v[i]; j++)
{
for (long k = 2;k<j;k++)
{
if(j%k==0)
{
break;
}
}
{
numPrime++;
fprintf(fp,"%d,",j);
}
}
fprintf(fp,"\r素数个数为：%d\r\r",numPrime);
}
fclose(fp);

v.clear();
return 0;
``````

}

2
3
10
72
83
59

0至2的素数：2,

0至3的素数：2,3,

0至10的素数：2,3,5,7,

0至72的素数：2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,

0至83的素数：2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,

0至59的素数：2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,