ZeroJudge a121.質數又來囉 (C++打法) === ###### tags: `ZeroJudge題目` ### 內容 你的好朋友質數先生又來找你囉,給你兩個數字,請算出這兩個數字包含的範圍內有幾個質數。 ### 輸入 輸入兩個正整數 a,b(1<=a<=b<=100000000)。 保證 b-a<=1000 ```clike= 3 7 6 6 30 50 ``` --- ### 輸出 輸出一個非負整數,代表 a 到 b 之間(包含 a,b)總共有幾個質數。 ```clike= 3 0 5 ``` --- ### 範例code ```clike= #include <iostream> using namespace std; int main() { int num1, num2; while(cin >> num1 >> num2) { int num = 0, i = num1; for(; i < 2 && i <= num2; i ++) ; for(;i <= num2; i++) { num++; for(int j = 2; j * j <= i; j++) { if(i % j == 0) { num--; break; } } } cout << num << endl; } return 0; } ```