Steps
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.
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

Sample Input
3 45 48 45 49 45 50
Sample Output
3 3 4
找规律题,没有想象中的那么复杂。
列出多个例子,归纳总结。
取两者差的开方值为n
1、在n*n-n+1到n*n之间,对应步数为2*n-1;
2、在n*n+1到n*n+n之间,对应步数为2*n;
3、在n*n+n+1到(n+1)*(n+1)之间,对应步数为2*(n+1)-1
代码如下:
#include <stdio.h> #include <math.h> int main(void) { int n,a,b,i; scanf("%d",&i); while(i--) { scanf("%d%d",&a,&b); if(a==b) { printf("0\n"); continue; } n=(int)pow(b-a,0.5); if(n*n+1>b-a) n=2*n-1; else if(n*n+n<b-a) n=2*n+1; else n*=2; printf("%d\n",n); } return 0; }