Sum

描述:


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.


输入:

The only line contains in the first line a positive integer S (0< S <= 100000) which represents the sum to be obtained.

输出:

The output will contain the minimum number N for which the sum S can be obtained.



输入样例:

12
0


输出样例:

7
3
解题思路:先对1~N求和,求出最大的范围,然后每当减一个数就是要减掉两倍的,就是减去偶数,所以只要Sum-sum是偶数即可。
代码如下:
Code:
  1. #include<iostream>
  2. usingnamespacestd;
  3. intmain()
  4. {
  5. intstate=0;
  6. intn;
  7. cin>>n;
  8. for(inti=1;;i++)
  9. {
  10. ints=i*(i+1)/2;
  11. if(s<n)
  12. continue;
  13. if((s-n)%2==0)
  14. {
  15. cout<<i<<endl;
  16. break;
  17. }
  18. }
  19. return0;
  20. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值