Finding out the relationship between the steps and the distance can be solved.
- 0 steps can reach the maximum distance is 0
- 1 steps up to the distance is 1 (1)
- 2 steps up to the distance is 2 (1 1)
- 3 steps up to the distance is 4 (1 2 1)
- 4 steps up to the distance is 6 (1 2 2 1)
- 5 steps up to the distance is 9 (1 2 3 2 1)
- 6 steps up to the distance is 12 (1 2 3 3 2 1)
- ...... In this category
1#include <iostream>2#include <cstdio>3#include <cmath>4 #defineERROR 1e-105 using namespacestd;6 intMain () {7 intn, x, y;8 intdis, step;9 while(SCANF ("%d", &n)! =EOF) {Ten for(inti =0; I < n; i++ ){ Onescanf"%d%d", &x, &y); A -dis = yx; - if(Dis = =0 ){ theprintf"0\n" ); - Continue; - } - +Step = (int) (sqrt (Double) dis) +ERROR); - if(Step * = = dis) Step = step *2-1; + Else if(Step * step + step < dis) Step = step *2+1; A ElseStep = Step *2; at -printf"%d\n", step); - } - } - return 0; -}
Other:
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 intx, y;7 inttestcases;8 intMin_steps =0;9CIN >>testcases;Ten while(Testcases--) One { ACIN >> x >>y; - intdifference = y-x; -Min_steps =0; the - if(Difference! =0) - { - intSumofsteps =0; + intz =2;//divided by 2, it represents the size if the next step - + while(Difference >sumofsteps) A { atSumofsteps + = (z/2);//Next Step -Min_steps + +; -z++; - } - } -cout << min_steps <<Endl; in } - return 0; to}
1#include <cstdio>2#include <cmath>3 4 intMainvoid)5 {6 intT, x, Y, diff, N;7scanf"%d", &t);8 while(t--)9 {Tenscanf"%d%d", &x, &y); Onediff = y-x; A if(diff = =0) -Puts"0"); - Else the { -n = (int) sqrt (diff); -diff-= n *N; - if(diff = =0) +printf"%d\n", (n <<1) -1); - Else if(diff <=N) +printf"%d\n", N <<1); A Else atprintf"%d\n", (n <<1) +1); - } - } - return 0; -}
UVa 846-steps