当前位置:网站首页>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<<' ';
}
边栏推荐
- 13.
lt.647. Palindromic substring + lt.516. Longest palindrome subsequence - Interface test framework combat (1) | Requests and interface request construction
- 【 Harmony OS 】 【 ano UI 】 lightweight data storage
- 在树莓派上搭建属于自己的网页(2)
- How to prepare for the test interface test data
- ModelArts第二次培训
- Talking about GIS Data (6) - Projected Coordinate System
- js实现一个 bind 函数
- Newifi路由器第三方固件玩机教程,这个路由比你想的更强大以及智能_Newifi y1刷机_smzdm
- 3. 无重复字符的最长子串
猜你喜欢

High availability, two locations and three centers

js实现一个 bind 函数

idea使用@Autowired注解爆红原因及解决方法

JS学习笔记(三)

Tributyl-mercaptophosphane "tBuBrettPhos Pd(allyl)" OTf), 1798782-17-8

Common lipophilic cell membrane dyes DiO, Dil, DiR, Did spectrograms and experimental procedures

Djiango第四次培训笔记

HarmonyOS应用开发第一次培训

快速上手 Mockito 单元测试框架

Redis6学习笔记
随机推荐
Object类与常用API
【Harmony OS】【ARK UI】ets use startAbility or startAbilityForResult to invoke Ability
Flask Web 报错:
web安全-SSTI模板注入漏洞
Create a tree structure
minio下载文件乱码或者是一条横线
Interface Test Framework Practice | Process Encapsulation and Test Case Design Based on Encrypted Interface
Presto installation and deployment tutorial
ss-5.consul服务端+生产者+消费者
ss-2.子项目互相访问(order80 -> payment8001)
Detailed explanation of MOSN reverse channel
1058 选择题 (20 分)(C语言)
1. 两数之和
【Biotin Azide|cas:908007-17-0】Price_Manufacturer
内部类、static关键字、final
ss-4.1-1个eurekaServer+1个providerPayment+1个consumerOrder
1089 狼人杀-简单版 (20 分)
Shell conditional statement judgment
celery工作原理图
GIS数据漫谈(六)— 投影坐标系统