说说:此题要求求出从整数x到达整数y所要经过的最短步数,且第一步和最后一步必须为一,同一时候每一步都比前一步多一步,少一步或一样。如果想搞清楚每一步详细是如何走的,那么这道题是相当麻烦的。考虑到前后两步之间最多差一,那么一到最大数之间的每个数从左到右以及从右到左的时候必然都出现。那么我们能够预想如果这样一个数列。

1,2,3,....n-2,n-1,n,n-1,n-2....3,2,1,若数列的和小于y和x的差肯定是不行的。所以要调整n至刚好大于或等于y和x的差。显然当y与x的差恰好为数列的和那直接输出结果。否则构成y和x差的数列的最大值必然为n-1。这时,由于步数要最少,所以我们从最大的那个n-1開始一个一个尽量多的放数。终于把y-x的差放完之后,就可以求得最短步数。例:y-x=7,则可找到1,2,3,2,1是大于9的,那么8的数列中必存在1,2,1这种序列,这时7-4=4,最多能再放一个2。那还剩下一个1,最后填入就可以。

题目:

Steps

One steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or

如今你要通过几步从数轴的某点到达另外一点。每步的长度都是非负的并能够比前一步大一步,相等或小一步

by one smaller than the length of the previous step.

What is the minimum number of steps in order to get from x to y? The length of the first and the last step must be 1.

从x到达y的最少步数是多少?而且第一步和最后一步的长度一定为1

Input and Output

Input consists of a line containing n, the number of test cases. For each test case, a line follows with two integers: 0xy <
231. For

输入有一行包括一个整数n,是測试的组数。对于每组測试数据,一行有两个整数0xy <
231。对于每行測试数据

each test case, print a line giving the minimum number of steps to get from x to y.

输出一行给出从x到y的最少步数。

Sample Input

3
45 48
45 49
45 50

Sample Output

3
3
4

源码:

#include <stdio.h>

int n,x,y,q;

int main(){
int i,j,k,result; // freopen("input.txt","r",stdin);
scanf("%d",&n); while(n--){
scanf("%d%d",&x,&y); q=y-x;
if(q<=3){//小于三的特殊情况先处理
printf("%d\n",q);
continue;
} for(i=2;;i++)
if(i*i>=q)
break; if(i*i==q){
printf("%d\n",2*i-1);
continue;
}
else
i--; q-=i*i; result=2*i-1;
while(q){//从数列中最大的数開始放,放完为止
result+=q/i;
q=q-i*(q/i);
i--;
} printf("%d\n",result);
}
return 0;
}

Steps UVA 846的更多相关文章

  1. uva 846 - Steps

    找出步數與距離的關係即可得解. 0步最多能抵達的距離是0 1步最多能抵達的距離是1(1) 2步最多能抵達的距離是2(1 1) 3步最多能抵達的距離是4(1 2 1) 4步最多能抵達的距離是6(1 2 ...

  2. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  3. 【转】UVa Problem 100 The 3n+1 problem (3n+1 问题)——(离线计算)

    // The 3n+1 problem (3n+1 问题) // PC/UVa IDs: 110101/100, Popularity: A, Success rate: low Level: 1 / ...

  4. PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版

    , '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...

  5. uva 116 Unidirectional TSP (DP)

    uva 116 Unidirectional TSP Background Problems that require minimum paths through some domain appear ...

  6. UVA - 1153 Keep the Customer Satisfied(贪心)

    UVA - 1153 Keep the Customer Satisfied Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. UVa 10795 - A Different Task 对称, 中间状态, 数位DP 难度: 3

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. [uva] 10067 - Playing with Wheels

    10067 - Playing with Wheels 题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Ite ...

随机推荐

  1. android中Logcat的深层理解

    Android的开发也能够归类为嵌入式设备的开发.即便不是嵌入式开发,依旧要注意对内存和处理的使用.养成一个好的习惯对自己的帮助是非常大的. 在Log的源代码中能够看到这种凝视: The order ...

  2. STM32建立project库函数方法

    (1)打开keilMDK主界面能够看到project中有一个默认的project,点击这个project名字,然后选择菜单Project->Close Project,就关闭掉这个project ...

  3. hibernate.cfg.xml文件的配置模板和不同数据库的配置參数

    (1)hibernate.cfg.xml文件的配置模板 <?xml version="1.0" encoding="UTF-8"?> <!DO ...

  4. Microsoft2013校园招聘笔试题及解答

    继续求拍砖!!!! 1. You are managing the database of a book publichser, you currently store the book orders ...

  5. java输出换行的标准姿势"line.separator"

    java中写.txt文件,实现换行的几种方法: 1.使用java中的转义符"\r\n": windows下的文本文件换行符:\r\n linux/unix下的文本文件换行符:\r ...

  6. IBinder在进程之间传递一个对象的形式(一)

    主张 什么时候service通常被称为远程时的,用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind service成 ...

  7. 浅谈 PHP 神盾的解密过程

    原文:浅谈 PHP 神盾的解密过程 前些日子一个朋友丢了个shell给我,让我帮忙解密,打开源码看了下写着是 “神盾加密” , 牛逼闪闪的样子.百度下发现神盾是个很古老的东西,最后一次更新是在 201 ...

  8. 在gem5的full system下运行 alpha编译的测试程序 running gem5 on ubuntu in full system mode in alpha

    背景 先需要在full system下运行gem5,通过网上查找资料以及向别人请教,终于成功运行,网上大多是关于alpha指令集的,且都是英文的,为了方便大家学习,现在总结一下,希望对大家有所帮助. ...

  9. CC 3-Palindromes(manacher)

    传送门:3-Palindromes 题意:求为回文串且能整除3且不前导0的子串个数. 分析:由 manacher算法O(N)可算出以i为坐标的最长为p[i]回文子串,且Si-k,Si-k+1..... ...

  10. 完整导出IntelliJ IDEA的快捷键

    工欲善其事,必先利其器. 常常和代码打交道的人,熟练使用IDE快捷键那是必须的,由于快捷键能够把你从各种罗嗦事中解放出来.比方,假设没有快捷键,你就须要常常性的暂停快速执行的大脑,右手凭记忆摸到鼠标, ...