当前位置:网站首页>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<<' ';
}
边栏推荐
猜你喜欢
idea uses @Autowired annotation to explain the reasons and solutions
接口和抽象
js实现一个 bind 函数
网络流媒体下载的 10 种方法(以下载 Echo 音乐为例)
在树莓派上搭建属于自己的网页(1)
PotPlayer实现上班摸鱼电视自由
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
Flask的简单介绍及使用方法简介
传说中可“免费白拿”的无线路由器 - 斐讯 K2 最简单刷 breed 与第三方固件教程
Kaggle(四)Scikit-learn
随机推荐
Js学习笔记(四)
HarmonyOS应用开发培训第二次作业
typescript39-class类的可见修饰符
HarmonyOS应用开发第一次培训
FileZilla 搭建ftp服务器
斐讯K2路由编译Padavan华硕固件和心得
曲线特征----曲线弯曲程度的探究
ModelArts第二次培训
junit总结
Interface test Mock combat (2) | Combined with jq to complete batch manual Mock
Power button 561. An array of split
【Flask】Flask-SQLAlchemy的增删改查(CRUD)操作
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
How to prepare for the test interface test data
传说中可“免费白拿”的无线路由器 - 斐讯 K2 最简单刷 breed 与第三方固件教程
快速上手 Mockito 单元测试框架
[Harmony OS] [ARK UI] ETS context basic operations
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
IO流及其操作