#include <iostream> #include <vector> using namespace std; #define MAX 1000000 long s[MAX+1]; void init()//构造一个100万以内的素数表 { int i, j; for (i = 2; i <= MAX; i++) { s[i] = 1; } s[0] = s[1] = 0; //0和1肯定不是素数 for (i = 2; i <= MAX; i++) { if (s[i] == 1) { for (j = 2 * i; j <= MAX; j += i) s[j] = 0; } } } int main() { init();//构造一个100万以内的素数表 long a, b; int tmp = 0; int i, j; vector<int> vec; while (cin >> a >> b) { for (i = a; i <= b; i++) { if (s[i] == 1) tmp++; } vec.push_back(tmp); tmp = 0; } i = 1; for (vector<int>::iterator it = vec.begin(); it != vec.end(); it++) { cout << "Case " << i++ << ":" << endl; cout << *it << endl; } //system("pause"); return 0; }
时间: 2025-01-10 03:58:02