[MM36-易] Prime Numbers
Problem Description
An integer n is said to be a prime number if it is greater than 1 and has only factors of 1 and itself. For example, 2, 3, 5, and7 are prime numbers, but 4, 6, 8 and 9 are not. This problem asks you to calculate the number of prime numbers less than a given integer N.
Technical Specification
2 ≤ N ≤ 30000
Input File Format
The input data consists of several lines, each line contains one integer 2 ≤ N ≤ 30000. The end of input is indicated by a number 0. There are at most 6 test cases.
Output Format
For each case, print the number of primenumbers less than N.
Example
An integer n is said to be a prime number if it is greater than 1 and has only factors of 1 and itself. For example, 2, 3, 5, and7 are prime numbers, but 4, 6, 8 and 9 are not. This problem asks you to calculate the number of prime numbers less than a given integer N.
Technical Specification
2 ≤ N ≤ 30000
Input File Format
The input data consists of several lines, each line contains one integer 2 ≤ N ≤ 30000. The end of input is indicated by a number 0. There are at most 6 test cases.
Output Format
For each case, print the number of primenumbers less than N.
Example
| Sample Input: | Sample Output: | 
| 3 10 15 32 0  | 1 4 6 11  | 
- #include <iostream>
 - #include <string>
 - #include <sstream>
 - #include <stdio.h>
 - #include <ctype.h>
 - #include <cmath>
 - using namespace std;
 - int main() {
 - // [MM36-易] Prime Numbers
 - string Num;
 - while(getline(cin, Num))
 - {
 - int num = stoi(Num);
 - int count = 0;
 - if(num != 0)
 - {
 - for(int i = 1;i < num;i++)
 - {
 - int c = 0;
 - for(int j = 1;j <= i;j++)
 - {
 - if(i % j == 0)
 - {
 - c++;
 - }
 - }
 - if(c == 2 && num != 1)
 - {
 - count++;
 - }
 - }
 - cout << count << endl;
 - }
 - else
 - {
 - break;
 - }
 - }
 - return 0;
 - }
 
沒有留言:
張貼留言