Sum of Consecutive Prime Numbers UVA
来源:互联网 发布:iphone4s数据恢复 编辑:程序博客网 时间:2024/06/17 12:18
首先使用打表法将范围内的所有的素数都求出来,然后利用互动窗口来求解即可,具体实现见如下代码:
#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<cmath>#include<iomanip>#include<cstring>#include<sstream>#include<cstdio>#include<deque>#include<functional>using namespace std;const int maxn = 10010;vector<int> prime;bool vis[maxn];void Init(){memset(vis, 0, sizeof(vis));vis[0] = vis[1] = 1;int up = floor(sqrt(maxn) + 0.5);for (int i = 2; i <= up; i++){if (!vis[i]){for (int j = i*i; j <= 10000; j += i)vis[j] = 1;}}for (int i = 2; i <= 10000; i++)if (!vis[i]) prime.push_back(i);}int main(){Init();int N;while (cin >> N){if (N == 0) break;int l, r;l = r = 0;int sum = prime[0];int ans = 0;while (l<=r&&r<prime.size()&&prime[r]<=N){if (sum == N){ans++;sum -= prime[l];l++;}else if (sum < N){r++;sum += prime[r];}else{sum -= prime[l];l++;}}cout << ans << endl;}return 0;}
阅读全文
0 0
- 【UVA】1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- Uva-1210 Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA - 1210 - Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers UVA
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers
- Sum of Consecutive Prime Numbers - UVa 1210 水题
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- Android NDK编译过程
- STM32随记
- 输入不同对象的姓名、性别、年龄、体重和住址等信息,并输出显示。
- 客户端怎么判断Socket连接已与服务器断开
- 【数据平台】centos下部署anaconda2和pyhs2组件
- Sum of Consecutive Prime Numbers UVA
- 行转列,多列的数据合并为一行,行转换
- node.js建站日记(2)
- 插入排序之直接插入排序
- Kubernetes 1.6.1 Kargo
- mybactis的一对多的思想与代码
- JAVA基础面试(一)
- Argparse模块:python快捷实现命令行操作
- 编程实现显示用户输入的月份(不包括闰年的月份)拥有的天数