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 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. Foreach test case, a line follows with two integers: 0![$ \le$](http://uva.onlinejudge.org/external/8/846img1.png)
![$ \le$](http://uva.onlinejudge.org/external/8/846img1.png)
Sample Input
3 45 48 45 49 45 50
Sample Output
3 3 4
我列个表解释把
1 1 1
1 2 1
1 2 1 1
1 2 2 1
1 2 2 1 1
1 2 2 2 1
1 2 3 2 1
1 2 3 2 1 1
1 2 3 2 2 1
1 2 3 3 2 1
每次走的步数大小与上一步相差最多为1, 不然步子突然迈大容易摔倒 哈哈~
看表即知, 我们可以从两边往中间走~ 两边从1开始
AC代码:
#include<stdio.h>
int main() {
int T;
scanf("%d", &T);
while(T--) {
int stard, end;
int dis;
int mark = -1;
int step = 1;
int num_step = 0;
scanf("%d %d", &stard, &end);
dis = end - stard;
while(dis > 0) {
dis = dis - step;
num_step++;
if(mark == 1)
step++;
mark = mark * (-1);
}
printf("%d\n", num_step);
}
return 0;
}