当前位置:网站首页>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<<' ';
}
边栏推荐
猜你喜欢
shell脚本循环语句
tag单调栈-单调栈预备知识-lt.739. 每日温度
Interface Test Framework Practice (4) | Get Schema Assertion
FileZilla 搭建ftp服务器
Online password generator tool recommendation
Unity2D horizontal board game tutorial 6 - enemy AI and attack animation
如何不耍流氓的做运维之-SHELL脚本
MCM box model modeling method and source analysis of atmospheric O3
breed Web刷机升级详细教材修正编译器固件说明_itkeji.top
第四次培训
随机推荐
Js学习笔记(四)
typescript46-函数之间的类型兼容性
Odps temporary query can write SQL, turned out to a named?
Practical application of WebSocket
用户密码加密工具
web安全-PHP反序列化漏洞
ss-3.工程重构
详解Nurbs曲线
Pr第二次培训笔记
生活原则。
typescript44-对象之间的类兼容器
Interface Test Framework Practice (4) | Get Schema Assertion
1. 两数之和
web安全-sql注入漏洞
Interface testing framework combat (3) | JSON request and response assertion
Flask的简单介绍及使用方法简介
breed Web刷机升级详细教材修正编译器固件说明_itkeji.top
User password verification
ModelArts第二次培训
MCM box model modeling method and source analysis of atmospheric O3