pds 1112 bfs

- -数据水吧...0S过

1112: 最少操作数

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 36   Solved: 28
[ Submit][ Status][ Web Board]

Description

最近,Dr. Kong 又新设计了一个机器人Bill。这台机器人很聪明,会做许多事情。比如你给它任意一个正整数,它可以按照一定的运算规则, 计算出得到1的最少操作次数。

该运算规则是:

               如果n为偶数,将其除以2;

               如果n为奇数,可以加1或减1;

               然后一直处理下去,直到得到运算结果为1。

现在假如你给定Bill一个正整数m,你认为它会怎么计算出计算出得到1的最少操作次数。

Input

 第一行:n 表示有多少组测试数据。

 接下来有n行:每一行有一个正整数m(0<m<100000),表示你给定Bill的正整数。

Output

对于每一行测试数据,输出一行,每行数据为最少操作次数。

Sample Input

37155

Sample Output

453

HINT


#include <cstdio>
#include <iostream>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
struct fuck
{
	int x;
	int step;
};
void bfs(int t)
{
	queue <fuck> q;
	fuck now,next;
	now.x=t;
	now.step=0;
	q.push(now);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x==1){
			cout<<now.step<<endl;
			break;
		}
		if(now.x%2==0)
		{
			next.x=now.x/2;
			next.step=now.step+1;
			q.push(next);
		}
		else {
			next.step=now.step+1;
			next.x=now.x+1;
			q.push(next);
			next.x=now.x-1;
			q.push(next);
		}
	}
	while(!q.empty())
		q.pop();
}


int main()

{
	//freopen("input.txt","r",stdin);
	int n;
	cin>>n;
	while(n--)
	{
		int t;
		cin>>t;
		bfs(t);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值