The ? 1 ? 2 ? ... ? n = k problem 

Theproblem

Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k
? 1 ? 2 ? ... ? n = k

For example: to obtain k = 12 , the expression to be used will be:
- 1 + 2 + 3 + 4 + 5 + 6 - 7 = 12
with n = 7

TheInput

The first line is the number of test cases, followed by a blank line.

Each test case of the input contains integer k (0<=|k|<=1000000000).

Each test case will be separated by a single line.

The Output

For each test case, your program should print the minimal possible n (1<=n) to obtain k with the above formula.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

2

12

-3646397

Sample Output

7

2701

题意不累赘~

做法:

假设sum1 = a1 + a2 + a3 + ... + an + x >= k

而sum2 = a1 + a2 + a3 + ... + an - x = k

那么sum1 - sum2 = 2x

也就是说, 无论k的正负, 全把k当正数处理, 一直累加正数得到sum1 与 不按全当正数处理得到的sum2 相差的值是一个偶数(2x, 即负数的绝对值的两倍~)

故, 全部从1累加到n吧, 直到 (sum >= k && (sum - k) % 2 == 0)

AC代码:

#include<stdio.h>

int T;

int main() {
scanf("%d", &T);
while(T--) {
int k;
int sum = 0;
scanf("%d", &k);
if(k < 0)
k = (-1 * k);
for(int i = 1; ;i++) {
sum += i;
if(sum >= k && (sum-k) % 2 == 0) {
printf("%d\n", i);
break;
}
}
if(T)
printf("\n");
}
return 0;
}

UVA 10025 (13.08.06)的更多相关文章

  1. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  2. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  3. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  4. UVA 10790 (13.08.06)

     How Many Points of Intersection?  We have two rows. There are a dots on the toprow andb dots on the ...

  5. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  6. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  7. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  8. UVA 424 (13.08.02)

     Integer Inquiry  One of the first users of BIT's new supercomputer was Chip Diller. Heextended his ...

  9. UVA 10106 (13.08.02)

     Product  The Problem The problem is to multiply two integers X, Y. (0<=X,Y<10250) The Input T ...

随机推荐

  1. SqlServer 列的增加和删除

    有些时候我们需要删除或增加数据库中有数据中表的列.总结一下列的删除和增加. 1. 删除列 当表中存在数据时,删除列后,数据也会被删除. sql语句: alter table 表名 drop colum ...

  2. .net core 使用

    在本机上安装了 visual studio 2015后,还要安装 DotNetCore.1.0.1-VS2015Tools.Preview2.0.3.exe 才能编译 .net core 的代码.不然 ...

  3. Anychart 破解备注

    由于项目里用到anychart组件,第一次破解了,后来升级再破解时忘了方法,所以在这里备注一下. 首先需要的工具: swfc  (http://www.buraks.com/swifty/swfc.h ...

  4. Linux securecrt破解

    其实,以前接触过破解的东西,但是很多东西早就忘记了,何况是在Linux环境下. 结果我常识更改时间,哦,不是更改日期,往后推4天,结果显示了26 days remaining. 所以完全可以更改日期来 ...

  5. python实现不可修改的常量

    因为种种原因,Python并未提供如C/C++/Java一样的const修饰符,换言之,python中没有常量,至少截止2015年年末,还没有这个打算.Python程序一般通过约定俗成的变量名全大写的 ...

  6. chrome 浏览器 开发者工具 性能检测 参数解释

    Sending is time spent uploading the data/request to the server. It occurs between blocking and waiti ...

  7. 通过ajax提交form表单

    $.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST" ...

  8. 基于AWS的自动化部署实践

    过年前,我给InfoQ写了篇文章详细介绍我们团队在过去4年基于AWS的自动化部署实践.文章包括了:为什么选择AWS.AWS上自动化部署的优势和挑战.我们的解决方案,以及和AWS DevOps方案(Op ...

  9. carthage 简单使用步骤

    brew install carthage切至项目目录:cd xxx创建Cartfile文件vi Cartfile填写依赖git "https://xxxxx" "mas ...

  10. 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误

    原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...