[3782]:xxx定律
2016-03-24 00:53
204 查看
Problem Description
对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。
请计算需要经过几步才能将n变到1,具体可见样例。
Input
测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
Output
对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
Sample Input
3
1
0
Sample Output
5
0
简单题
对于一个数n,如果是偶数,就把n砍掉一半;如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止。
请计算需要经过几步才能将n变到1,具体可见样例。
Input
测试包含多个用例,每个用例包含一个整数n,当n为0 时表示输入结束。(1<=n<=10000)
Output
对于每组测试用例请输出一个数,表示需要经过的步数,每组输出占一行。
Sample Input
3
1
0
Sample Output
5
0
简单题
/* author : Yangchengfeng */ #include<stdio.h> int main() { int n; while(scanf("%d", &n)!=EOF){ int time = 0; if(n == 0){ break; } else { while(n != 1){ if(n % 2 == 0){ n /= 2; } else { n = (3 * n + 1) / 2; } time++; } printf("%d\n", time); } } return 0; }
相关文章推荐
- 打包发布流程
- 作业3.2:psp
- java多线程同步案例之--生产者消费者模型
- 放养的小爬虫--豆瓣电影入门级爬虫(mongodb使用教程~)
- C# asp.net mvc 配置多个route 参数
- Python面向对象编程基础学习笔记
- 细谈c#游戏编程实例---菜鸟日记
- ios/swift上传代码到CocoaPods
- DexClassLoader 插件
- QThread 的wait()的用法
- PSP记录个人项目耗时情况
- Android中使用java.util.Properties犯的错
- windows下多个python版本共存
- 我与小娜(41):人工智能辅助写作
- Matlab padarray函数详解
- ARC 下处理内存暴涨的一个解决办法
- Block实现-block的实质
- PHP中的魔术方法:__construct, __destruct , __call,__get, __set, __isset, __unset , __toString, __set,__clone and __autoload
- 最短路径算法专题2----Dijkstra
- ThinkPHP3.2使用import来new对象时的异常