2595 X to Y of thinking

Small class Y silly goof off, he wrote two random numbers on paper X and Y. Now he wants to become a Y X in some way, in order to make this process more interesting, so he limited himself only use two modes of operation:

1, the current X-by-two

2, a Save the current X

Because of the limited time in the classroom, so he used the least number of operations to achieve this process. After class, passing qz see a small piece of paper that says X and Y Y, he will know within a second small Y with the number of operations. So the question is, with a small Y in the end how many times does it work?

 

Entry

Two numbers X and Y represents 
where 
. 1 <= X <= 1,000,000,000 
. 1 <= Y <= 1,000,000,000

Export

A number that is the X becomes Y requires a minimum number of operations

SAMPLE INPUT

1 1000000000

Sample Output

39 


Consider the y become x, then only two modes of operation: 1.y / = 2 2.y + = 1;
if y is an odd number, then only the second execution: y + = 1 becomes an even number
when y is even, consider it into Y / 2, then in addition to direct; if it becomes y / 2 + 1, first is coupled to an addition, the operation of step 2; otherwise then add 2/2, the operation of step 3;
so for an even number, in addition to better together;
then the entire solution came out: for an even number of on / 2, in the case of y <x, it is plus (xy) can;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<map>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
const int maxn = 2010;
typedef long long ll;
#define rdint(x) scanf("%d",&x)
#define inf 0x3f3f3f3f
int x, y;

int main()
{
	rdint(x); rdint(y);
	int minn = inf;
	int cnt = 0;
	while (1) {
		if (x == y)break;
		if (y % 2 == 0 && y > x) {
			cnt++; y /= 2;
		}
		else if (y > x&&y % 2 == 1) {
			cnt++; y += 1;
			Cnt + = (x - y);
		}
		else if (y < x) {
			y = x; break;
		}
	}
	cout << cnt << endl;
	system("pause");
//	return 0;
}

  


Guess you like

Origin www.cnblogs.com/zxyqzy/p/11128695.html