Sunday, August 1, 2010

Higher n higher



Kapil is climbing the steps of his
office after meeting Anshuman. The length of a step must be nonnegative and can
be by one bigger than, equal to, or one smaller than the length of the previous
step. Since his time is very precious he needs to do this using the minimum
number of steps in order to get from x to y. He needs your help for designing
an algorithm for this feat. The length of the first and the last step must be
one.



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 < x < y < 2^31. For each test case, print a line giving
the minimum number of steps to get from x to y.



Sample Input 





3

17
41



1
41



14604
32391



 



Sample Output 



9



12



266



#include<iostream>



using
namespace std;



int main()



{



    long int test,num1,num2,incr,decr,count;



                cin>>test;



                while(test>0)



                {



                                count=0;



                                incr=1;decr=1;



                                cin>>num1;



                                cin>>num2;



                                while(num1<num2)



                                {



                                                num1=num1+incr;



                                                incr++;



                                                count++;



                                                if(num1<num2)



                                                {



                                                                num2=num2-decr;



                                                                decr++;



                                                                count++;



                                                }



                                }



                                cout<<count<<endl;



                                test--;



                }



                return
0;



}

1 comment: