当前位置:网站首页>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<<' ';
}
边栏推荐
- 第三次HarmonyOS培训
- 高效率科研神器——小软件、大能量
- Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
- ss-3.工程重构
- Online password generator tool recommendation
- BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier
- celery工作原理图
- Interface testing framework combat (3) | JSON request and response assertion
- Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures
- Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
猜你喜欢
随机推荐
反射注解基础
曲线特征----曲线弯曲程度的探究
Pr第四次培训笔记
1. 两数之和
初步认识ZK
flask 面试题 问题
用户密码验证
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
2. 两数相加
Flask Web 报错:
js implements a bind function
深度学习入门之GRU
第四次培训
web安全-sql注入漏洞
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
Secondary development of WinForm controls
13.< tag-动态规划和回文字串>lt.647. 回文子串 + lt.516.最长回文子序列
1060 爱丁顿数 (25 分)
Create a tree structure
C# async and multithreading









