当前位置:网站首页>3n+1问题
3n+1问题
2022-08-03 05:10:00 【-JMY-】
题目描述
•对于任意大于1的自然数n,若n为奇数,则将n变为3n+1,否则变为n的一半。经过若干次这样的变换,一定会使n变为1。例如3→10 →5 →16 →8 →4 →2 →1。
•1<n<=2147483647
•输入n,输出变换过程,每个数后面输出一个空格。
•例如,输入3,输出
•3 10 5 16 8 4 2 1
样例输入
3
样例输出
3 10 5 16 8 4 2 1
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
long long a;
cin>>a;
while(a!=1){
cout<<a<<' ';
if(a%2==1)
a=a*3+1;
else
a/=2;
}
cout<<1<<' ';
}
边栏推荐
- Flink state
- flask 面试题 问题
- Interface testing framework combat (3) | JSON request and response assertion
- Presto installation and deployment tutorial
- Redis常用命令
- C# async and multithreading
- Exception (abnormal) and Error (error) difference analysis
- Harmony OS Date ano UI 】 【 】 the basic operation
- MCM box model modeling method and source analysis of atmospheric O3
- Shell conditional statement judgment
猜你喜欢
随机推荐
[Fine talk] Using native js to implement todolist
junit总结
shell脚本循环语句
集合框架知识
建造者模式(Builder Pattern)
GIS数据漫谈(五)— 地理坐标系统
typescript40-class类的保护修饰符
Talking about GIS Data (6) - Projected Coordinate System
HarmonyOS应用开发第一次培训
Lambda表达式案例
Two ways to simulate multi-user login in Jmeter
详解Nurbs曲线
4.如何避免缓存穿透、缓存击穿、缓存雪崩
VSO Downloader Ultimate 5.0.1.45 中文多语免费版 在线视频下载工具
typescript46-函数之间的类型兼容性
用户密码加密工具
Business table analysis - balance system
《录取通知》 观后感
flask 面试题 问题
Odps temporary query can write SQL, turned out to a named?








![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
