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. 【LeetCode】107 - Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. 数往知来 SQL SERVER 基本语法<七>

    sqlserver学习_01 启动数据库 开始->cmd->进入控制台    sqlcmd->-S .\sqlexpress    1> 如果出现表示数据库"sqle ...

  3. 开扒php内核函数,第二篇 hex2bin

    从上一篇我们得知怎样把ascii变成16进制显示,这篇我们是怎样把16进制变成ascii显示 我们还是从分析开始吧 先看这个函数的介绍吧 string hex2bin ( string $data ) ...

  4. iOS优秀博客收录(持续更新)

    唐巧 王巍 破船之家 NSHipster Limboy 无网不剩 念茜的博客 Xcode Dev Ted’s Homepage txx’s blog KEVIN BLOG 阿毛的蛋疼地 亚庆的 Blo ...

  5. 使用ReflectionTestUtils解决依赖注入

    概述   当使用junit来测试Spring的代码时,为了减少依赖,需要给对象的依赖,设置一个mock对象,但是由于Spring可以使用@Autoware类似的注解方式,对私有的成员进行赋值,此时无法 ...

  6. 自定义元素 – 在 HTML 中定义新元素

    本文翻译自 Custom Elements: defining new elements in HTML,在保证技术要点表达准确的前提下,行文风格有少量改编和瞎搞. 原译文地址 本文目录 引言 用时髦 ...

  7. C# Common Keyword

    [C# Common Keyword] 1.abstract Use the abstract modifier in a class declaration to indicate that a c ...

  8. ARP防火墙绑定网关MAC地址预防ARP攻击和P2P终结者

    [故障原理]  要了解故障原理,我们先来了解一下ARP协议.  在局域网中,通过ARP协议来完成IP地址转换为第二层物理地址(即MAC地址)的.ARP协议对网络安全具有重要的意义.通过伪造IP地址和M ...

  9. linux which 查看可执行文件的位置

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索:        which  查看可执行文件的位置.       whereis 查看文件的位置.         ...

  10. 关于Unity

    14年左右的时候开始学习了Unity,一直没有时间总结一些东西,框架机制啥的都不用说了,网上到处都有,虽然Unity是脚本机制,但是熟悉编程的人只要理解透了拿面向对象的思维编码也完全没有问题,这里重新 ...