846 - Steps

来源:互联网 发布:知天下图片 编辑:程序博客网 时间:2024/06/04 16:46

  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 toy? The length of the first and the last step must be 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:0$ \le$x$ \le$y < 231. For each test case, print a line giving the minimum number of steps to get fromx to y.

Sample Input 

345 4845 4945 50

Sample Output 

334
#include<stdio.h>int step(int p){int sum=0;sum=(1+p/2)*(p/2);if(p%2) sum+=p/2+1;return sum;}int main(void){int n,x,y,d;scanf("%d",&n);while(n--){scanf("%d%d",&x,&y);d=y-x;if(!d) puts("0");else {int j;for(j=0;j<y;j++)if(d<=step(j)){printf("%d\n",j); break;}}}return 0;}