当前位置:网站首页>C语言 害死人不偿命的(3n+1)猜想
C语言 害死人不偿命的(3n+1)猜想
2022-06-30 01:25:00 【Martin の Blog】

算法设计思路
- 判断一个数是奇数还是偶数,可以采用:n%2 是否等于零的方式进行处理
- 计算需要砍多少下,定义一个count变量,其初始值为0,砍一次加一
- 输出count值
代码
#include <stdio.h>
int get_step(int n){
int count = 0;
while(n != 1){
if(n%2 == 0){
n /= 2;
}else if(n%2 == 1){
n = (3 * n + 1) / 2;
}
count++;
}
return count;
}
int main(){
int n;
scanf("%d", &n);
int steps = get_step(n);
printf("%d", steps);
return 0;
}
运行结果

边栏推荐
- 首届技术播客月开播在即
- App test related tools
- Seata 與三大平臺攜手編程之夏,百萬獎金等你來拿
- STC89C52 single chip microcomputer simple calculator design and code demonstration
- How to build your own blog website by yourself
- Seata and the three platforms are working together in the summer of programming. Millions of bonuses are waiting for you
- postman 之接口关联
- Newton method (optimization of two variable functions)
- MySQL function
- JS anti shake and throttling
猜你喜欢

Analysis of natural frequency and buckling load of cylinder by finite element method

Seata et les trois plateformes travaillent main dans la main pour programmer Summer, un million de bonus vous attend

我,33岁,字节跳动测试开发,揭开北京“测试岗”的真实收入

Pytroch Learning Notes 6: NN network layer convolution layer

城市规划馆在设计制作上需要注意什么

What is digital garbage? Follow the world's first AI artist to explore meta carbon Art

Cookie加密12

Stimulsoft Reports报告工具,Stimulsoft创建和构建报告

Analysis of IM instant messaging development technology on modern web

js返回内容被unicode编码
随机推荐
C language final without failing (Part 1)
存储引擎分析
The Web3 era is coming? Inventory of five Web3 representative projects | footprint analytics
Solution to webkitformboundaryk in post request
Ansible ad-hoc temporary command
Three text to speech artifacts, each of which is very practical
眼底出血术后需注意事项//每天必看
The first unlucky person watching eth 2.0
cookie加密8
【机器学习Q&A】准确率、精确率、召回率、ROC和AUC
Vant weave - remove (clear) < van button > button component Click to display gray background effect
How to build your own blog website by yourself
快手伸手“供给侧”,找到直播电商的“源头活水”?
3-6sql injection website instance step 5: break through the background to obtain web administrator permissions
Application of pointer in STC89C52 single chip microcomputer and demonstration of practical cases
2020-12-03
Mysql 监控5
MySQL installation steps (detailed)
RubyMine开发工具,重构和意图操作
[recommended] how to quickly locate a bug during testing