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. Golang做的验证码(2)

    前面一篇文章介绍了2个用Golang做的验证码 http://www.cnblogs.com/ghj1976/p/3392847.html  这里再补充几个: 1.在GAE上使用的Google的验证码 ...

  2. Folding

    题意: 给定一个串,求能化成的最短循环节串(把重复字符串转化成循环节形式) 分析: 不是太好想,如果让求最短长度还好,dp[i][j],表示区间[i,j]化成的最小长度,dp[i][j]=min(dp ...

  3. LoadRunner学习记录--安装遇到的问题一

    安装过程中的出现了此计算机上缺少vc2005_sp1_with_atl_fix_redist 需要到这个目录下lrunner\En\prerequisites\vc2005_sp1_redist\ 手 ...

  4. Nodejs_day02

    Nodejs的事件模块 var events = require('events'); var eventEmitter = new events.EventEmitter();//创建EventEm ...

  5. STL六大组件之——分配器(内存分配,好深奥的东西)

    SGI设计了双层级配置器,第一级配置器直接使用malloc()和free(),第二级配置器则视情况采用不同的策略:当配置区块超过128bytes时,视之为“足够大”,便调用第一级配置器:当配置区小于1 ...

  6. Java中的路径问题

    Java中的路径问题 代码说明,如下: package com.merlin.test; import java.io.InputStream; public class Test { public ...

  7. Github在windows7环境下使用入门

    1.下载并安装 下载和安装一般都没什么问题,网上的链接一大堆,不过还是在此给一个安装的地址和安装的参考吧. 当然,安装完成后要保证git能使用,必须配置github 2.配置github 首先是要创建 ...

  8. Python线程

    原文出处: AstralWind 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图所示: 1.2. 线程同步(锁) 多线程的优势在于可以同时运行多个任务(至少感觉起来是这样). ...

  9. linux 新学到的命令

    nohup python -u /data/daemon/daemon/run.py & 使py程序可以在后台运行 nohup php a.php & 在linux平台上,要在后台运行 ...

  10. perf