2739 -- Sum of Consecutive Prime Numbers
2008-10-18 23:36
288 查看
Keywords:Collecting prime numbers algorithm
1不算
遍历一遍,删掉2的倍数:得2 3 5 7 9 11 13 15 17 19 21 23 25
遍历一遍,删掉3的倍数:得2 3 5 7 11 13 17 19 23 25
遍历一遍,删掉5的倍数:得2 3 5 7 11 13 17 19 23
The key thinking is that when we delete all the non-prime numbers, we get prime numbers.
筛选法求素数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251不算
遍历一遍,删掉2的倍数:得2 3 5 7 9 11 13 15 17 19 21 23 25
遍历一遍,删掉3的倍数:得2 3 5 7 11 13 17 19 23 25
遍历一遍,删掉5的倍数:得2 3 5 7 11 13 17 19 23
The key thinking is that when we delete all the non-prime numbers, we get prime numbers.
/***************************************** * Problem No. 2739 * Algorithm: Prime Numbers Collecting * Date: Oct 18,2008 * Author: Jessie Zhang ******************************************/ #include <iostream> using namespace std; const int SIZE = 10000; const int minPrime = 2; int primesCnt = 0; //prime numbers table int prePrimes[SIZE + 1]; bool isPrime[SIZE + 1]; int main(void) { //initialize isPrime table for(int i = 2;i <= SIZE;i++) { isPrime[i] = true; prePrimes[i] = 0; } //get all primes int maxDivider = 100; for(int divider = 2;divider <= maxDivider;divider++) { if(!isPrime[divider]) continue; int maxMultiplier = SIZE / divider; for(int multiplier = 2;multiplier <= maxMultiplier;multiplier++) { isPrime[divider * multiplier] = false; } } for(int i = SIZE;i >= 3;) { int j = i; while(j >= 2) { j--; if(isPrime[j]) break; } for(int k = i;k > j;k--) { prePrimes[k] = j; } i = j; } int inputNum; int counter; int numOfAdders; int sum; while(true) { cin>>inputNum; if(inputNum == 0) break; if(inputNum == 1) { cout<<0<<endl; continue; } if(inputNum == 2) { cout<<1<<endl; continue; } counter = 0; if(isPrime[inputNum]) counter++; numOfAdders = 2; int index = prePrimes[inputNum]; while(true) { bool flag = false; sum = 0; int oldIndex = index; for(int i = 0;i < numOfAdders;i++) { if(index == 0) { flag = true; break; } sum += index; index = prePrimes[index]; } if(flag) break; if(sum == inputNum) { numOfAdders++; index = prePrimes[oldIndex]; counter++; } else if(sum < inputNum) { numOfAdders++; index = oldIndex; } else { index = prePrimes[oldIndex]; } } cout<<counter<<endl; } return 0; }
相关文章推荐
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers 小结
- POJ 2739(Sum of Consecutive Prime Numbers) 素数筛法+暴力 Java
- poj 2739 Sum of Consecutive Prime Numbers(尺取法)
- POJ 【2739】 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
- Sum of Consecutive Prime Numbers(POJ 2739)
- POJ 2739 · Sum of Consecutive Prime Numbers【尺取法】【筛法】
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 题目2739 Sum of Consecutive Prime Numbers(水题)
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers(尺取,暴力)
- poj-2739-Sum of Consecutive Prime Numbers
- 北大ACM2739——Sum of Consecutive Prime Numbers
- Greedy:Sum of Consecutive Prime Numbers(POJ 2739)
- POJ 2739 Sum of Consecutive Prime Numbers
- 【原】 POJ 2739 Sum of Consecutive Prime Numbers 筛素数+积累数组 解题报告
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表 + 暴力)
- POJ 2739 Sum of Consecutive Prime Numbers