POJ 1844 sum
来源:互联网 发布:win7系统禁止安装软件 编辑:程序博客网 时间:2024/06/05 14:31
题目来源
Description
有从 1 到 n 个数, 现在通过加号或者是减号将这 n 个数连接起来,这个表达式的值为 S . 现在给你一个 s, 让你求出最小的 n 是多少?
Input
输入S (0< S <= 100000)
Output
最小的 n
Sample Input
12
Sample Output
7
Hint
The sum 12 can be obtained from at least 7 terms in the following way: 12 = -1+2+3+4+5+6-7.
Source
Romania OI 2002
Code
#include <cstdio>#include <algorithm>#include <iostream>#include <cstring>#include <fstream>#include <map>#include <queue>#include <set>#include <cstdlib>#include <ctime>#include <vector>#include <cmath>#include <limits.h>using namespace std;#define MAXN 200000int dp[2][MAXN + 1];int main(){ int n ,i ,j; while(cin>>n) { memset(dp[1], 0, sizeof(dp[1]));//首先初始第一层结果集 dp[1][MAXN/2 + 1] = 1; dp[1][MAXN/2 - 1] = 1; n += MAXN/2; for(i=2 ; ; i++) { memset(dp[i&1], 0, sizeof(dp[i&1]));//初始当前结果集 for(j = 0; j <= MAXN; j++) { if(dp[(i-1)&1][j]) //在上一层结果集找 { if(j + i <= MAXN) dp[i&1][j + i] = 1;//在范围内则记录 if(j - i >= 0) dp[i&1][j - i] = 1; } } if(dp[i&1][n]) //找到n了就退出循环 { cout<<i<<endl; break; } } } return 0;}
阅读全文
0 0
- POJ 1844 Sum
- poj 1844 Sum
- poj 1844 Sum
- poj 1844 sum
- poj 1844 Sum
- poj 1844 Sum
- POJ 1844 Sum
- POJ 1844 Sum
- poj 1844 sum (数学)
- poj 1844 Sum 【数学】
- poj-1844-Sum
- POJ 1844 Sum
- [POJ]1844 Sum
- POJ 1844 sum
- POJ 1844 Sum 笔记
- POJ 1844 Sum【简单数学】
- POJ 1844 Sum(数学)
- poj 1844 Sum ( 数学思维 )
- linux下重启oracle服务:监听器和实例
- Excel VBA高效办公应用-第十章-高效处理学员资料-Part1 (成绩排名)
- json
- Redis 集群规范(中文稿)(MOVED错误码及ASK错误码
- 10. 反向代理设置
- POJ 1844 sum
- 论文笔记:Deep neural networks for YouTube recommendations
- 钙钛矿太阳能电池研究进展
- 剑指offer——二叉树中和为某一值的路径(不错)
- 网易笔试题:男女小孩站队问题
- C语言的指针兼容性讨论
- 自定义MVC
- js动态append添加的节点点击事件无效
- Apk源码的加固(加壳)原理解析和实现