zrq495
www.zrq495.com

最小步数   距离   

0      0

1      1

2      2

3      4

4      6

5      9

6      12

 

代码:

 

 1 #include<iostream>
 2 #include<cmath>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int T, n, m;
 9     int dis, step;
10     cin >> T;
11     while(T--)
12     {
13         cin >> n >> m;
14         dis=m-n;
15         if (dis == 0)
16         {
17             cout << "0" << endl;
18             continue;
19         }
20         step=(int)(sqrt((double)dis));
21         if (step * step == dis)
22             step=step*2 - 1;
23         else if (step * (step + 1) < dis)
24             step=step*2+1;
25         else
26             step*=2;
27         cout << step << endl;
28     }
29     return 0;
30 }
posted on 2012-07-26 17:10  zrq495  阅读(269)  评论(0编辑  收藏  举报