pds 1112 bfs
来源:互联网 发布:解放军的无人机 知乎 编辑:程序博客网 时间:2024/06/01 21:00
- -数据水吧...0S过
1112: 最少操作数
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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 longusing 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
- pds 1112 bfs
- pds安装的注意事项
- Create PDS and Member
- PDS and PDSE
- how to add a member into the PDS using JCL
- Cognos异常(四) - 计算溢出问题(PDS-PPE-0104)
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- c++字符串替代源码
- bee框架学习笔记
- Spring MVC3在controller和视图之间传递参数的方法
- 循序渐进开发WinForm项目(4)--Winform界面模块的集成使用
- -------------Android判断应用或Activity的存在性
- pds 1112 bfs
- 机器人
- 找回丢失已久的输入法状态条
- ZOJ3471
- iOS7Bug UINavigationController push一个controller的时候 会出现屏幕卡顿一下
- 对潜意识的理解(一)
- LeetCode Gray Code
- mac 中使用 AppleScript 来发送 iMessage
- Android 源码编译