当前位置:网站首页>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<<' ';
}
边栏推荐
- 用scikit-learn学习谱聚类
- Flink state
- Interface testing framework combat (3) | JSON request and response assertion
- Newifi路由器第三方固件玩机教程,这个路由比你想的更强大以及智能_Newifi y1刷机_smzdm
- typescript41-class类的私有修饰符
- 2017-06-11 Padavan 完美适配newifi mini【adbyby+SS+KP ...】youku L1 /小米mini
- HarmonyOS应用开发培训第二次作业
- 4.如何避免缓存穿透、缓存击穿、缓存雪崩
- 用户密码加密工具
- typescript47-函数之间的类型兼容性
猜你喜欢
随机推荐
Interface Test Framework Practice (4) | Get Schema Assertion
Object类与常用API
3. 无重复字符的最长子串
斐讯K2路由编译Padavan华硕固件和心得
【 Harmony OS 】 【 ano UI 】 lightweight data storage
Djiango第四次培训笔记
Online password generator tool recommendation
js implements a bind function
web安全-SSTI模板注入漏洞
C# async and multithreading
shell script loop statement
力扣561. 数组拆分
UV decomposition of biotin - PEG2 - azide | CAS: 1192802-98-4 biotin connectors
Talking about GIS Data (5) - Geographic Coordinate System
设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
js实现一个 bind 函数
Get the Ip tool class
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
快速上手 Mockito 单元测试框架
tag单调栈-单调栈预备知识-lt.739. 每日温度







