[POJ]1844 Sum
来源:互联网 发布:海尔智能软件下载 编辑:程序博客网 时间:2024/06/05 11:56
[POJ]1844 Sum
问题
Description
Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S. The problem is to determine for a given sum S the minimum number N for which we can obtain S by associating signs for all numbers between 1 to N.
For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem.Input
The only line contains in the first line a positive integer S (0< S <= 100000) which represents the sum to be obtained.
Output
The output will contain the minimum number N for which the sum S can be obtained.
Sample Input
12
Sample Output
7
分析
纯数学推导。。。。涉及到小学时学的高斯求和问题,即
因此情况就分为:
1.
2.
因此,对于输入的S,可以先确定使得
若
源代码
#include <iostream>#include <cmath>using namespace std;int main() { int S, i; while (cin >> S) { i = (int)sqrt(2.0 * S); if (i * (i + 1) < 2 * S) ++i; if ((i * (i + 1) / 2 - S) % 2 == 0) cout << i << endl; else { if (i % 2 == 1) cout << i + 2 << endl; else cout << i + 1 << endl; } } return 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 ( 数学思维 )
- UITextField与键盘returnSearch关联
- iOS警告收录及科学快速的消除方法
- Spark LDA 主题预测
- Arduino - RGB全彩LED模块
- mtk M8735平台android L版本修改记录
- [POJ]1844 Sum
- 手把手教你使用Git
- Android之层次statelist属性
- 236. Lowest Common Ancestor of a Binary Tree
- iOS提交后申请加急审核
- 开源项目之Android Afinal框架
- 从apache mod_php到php-fpm
- android studio 使用问题2
- 单链表代码3