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. 微信公众平台开发localStorage数据总是被清空

    我把现在项目中的用户数据存储过程改成本地的,只用localStorage,但是随之而来很多问题,原因就是localStorage只有很短的有效时间,退出公众号,关闭微信都会清空.最不能容忍的是用户还在 ...

  2. testng几个tips

    1. testng的测试方法不能有返回值,即必须是void返回值类型. 测试方法前加入了@Test, 但以testNG方式运行,run test为0 以下public WebDriver ...应改为 ...

  3. JNI编程,C++调用Java

    本地代码中使用Java对象 通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instance)的域,调用静态(static)和实例(instance)函 ...

  4. 精美&创意的WordPress新发主题集合

    今天我整理了一些精美&创意的WordPress新发主题,它们基本上都融合了最新的设计理念,从简约到响应式,应有尽有. Engo – Smart & Minimal WordPress ...

  5. 分布式文件系统--GFS

    分布式文件系统 分布式文件系统:当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区(partition)并存储到若干台单独的计算机上.管理网络中夸多台计算机存储的文件系统.这种系统 ...

  6. Strider SSH Deploy配置

    登录需要ssh, ssh 免密码登录配置自行百度.shell里写成自己的需要的命令

  7. Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

    mvn war:war命令出错: 原因: maven的web项目默认的webroot是在src\main\webapp.如果在此目录下找不到web.xml就抛出以上的异常. 解决方案: 在pom.xm ...

  8. ajax 新闻栏目

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. U盘分区信息清除

    diskpart select disk 1 clean 清除选中(优U)盘的所有信息;

  10. 第二百一十七天 how can I 坚持

    JavaScript  document.getElementByName()获取数组,for循环,搞了一天,好笨. 明天要下雪了,好冷. 双十一,天猫搞的挺特别啊,晚上抢了个小米红包,不知道买啥,哎 ...