描述:
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是偶数即可。
代码如下:
-
#include<iostream>
-
usingnamespacestd;
-
intmain()
-
{
-
intstate=0;
-
intn;
-
cin>>n;
-
for(inti=1;;i++)
-
{
-
ints=i*(i+1)/2;
-
if(s<n)
-
continue;
-
if((s-n)%2==0)
-
{
-
cout<<i<<endl;
-
break;
-
}
-
}
-
return0;
- }
分享到:
相关推荐
For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the ...
windows平台下的md5sum, 跟linux下的md5sum算出来的一模一样. 单文件, 直接在命令行下使用.
这个网站上很多md5的源代码都是有问题的,我就重提交一个跟md5sum运行效果一样的源码供大家参考
matlab函数sum和size用法-matlab函数sum和size用法.doc matlab函数sum和size用法.doc sum函数解释函数功能 求数组元素的总和 使用方法B = sum 返回数组A不同维数的总和。 如果A是一个...
Windows系统文件md5计算工具,md5sum命令。下载保存到C:\Windows或其他环境变量目录。
我们一般通过表达式$sum来计算总和。因为MongoDB的文档有数组字段,所以可以简单的将计算总和分成两种: 1,统计符合条件的所有文档的某个字段的总和; 2,统计每个文档的数组字段里面的各个数据值的和。这两种情况...
基于Labview的Sum Check/Sum校验程序
自研操作系统:DIM-SUM设计与实现文字版 自研操作系统:DIM-SUM设计与实现文字版 自研操作系统:DIM-SUM设计与实现文字版
LINQ多个Sum同时执行的方法,以提供效率。
ms sql server 字符合计,类似sum函数的功能,不过是针对字符型!
(1).熟悉tomcat的启动和关闭操作...2.参考教材的例子1,编写一个JSP页面sum.jsp,功能是求出表达式 1!+2!+3!+...+16! 的和,并显示在页面。编写好后分别放在根目录,自建Web服务目录,创建的虚拟目录中进行测试运行。
md5sum sha1sum for windows 微软官方md5sum和sha1sum工具,校验速度很快。
针对多个体系统在个体间进行信息交换时发生接收信息滞后,存在通信时延,影响优化算法的收敛速度的问题,提出一种时延情形下的分布式Push-sum次梯度优化算法,该方法在权矩阵不具有正对角线元素时仍适用,并应用系统扩维...
linux环境下,用于计算md5sum的小工具,通常也能轻易移植到嵌入系统
a program to sum array elements
跟linux当中的md5sum命令相同,但需要重新编译,gcc -ott md5sum.c
Excel常见函数sumif及SUM数组函数的应用 Excel 是一个功能强大的电子表格软件,具有强大的数据处理和分析能力。在实际工作中,Excel 的函数公式起着至关重要的作用,能够快速、准确地处理和分析数据。本资源概括了 ...
winMd5Sum ..... md5
1 case when 写法 2 sum case when 用法 3 select 拼接字段 示例如下: when 2 then 'C' else 'D' end ) as '类型',count(*) as '数量' from table group by orderType
windows版的md5sum.exe,命令行。 可以用于批处理脚本中。