当前位置:网站首页>PAT class B 1001 (3n+1) conjecture
PAT class B 1001 (3n+1) conjecture
2022-08-01 04:49:00 【Go to bed early and feel good hh】
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528
题目描述
卡拉兹(Callatz)猜想:
对任何一个正整数 n n n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 ( 3 n + 1 ) (3n+1) (3n+1) 砍掉一半.这样一直反复砍下去,最后一定在某一步得到 n = 1 n=1 n=1.卡拉兹在 1950 1950 1950 年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证 ( 3 n + 1 ) (3n+1) (3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……
我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过 1000 1000 1000 的正整数 n n n,简单地数一下,需要多少步(砍几下)才能得到 n = 1 n=1 n=1 ?
输入格式
每个测试输入包含 1 1 1 个测试用例,即给出正整数 n n n 的值.
输出格式
输出从 n n n 计算到 1 1 1 需要的步数.
输入样例
3
输出样例
5
题目解析
思路
This question is a simple simulation question.
根据题意,在 while
The loop does different operations for odd and even numbers respectively,Also record the number of cycles.while
循环退出时,输出循环次数即可.
位运算
The benefit of bitwise operations is that they are extremely fast,It runs more efficiently than division、Take a lot of excess.
判断 n n n 是奇数:n % 2 == 1
或 n & 1 == 1
判断 n n n 是偶数:n % 2 == 0
或 n & 1 == 0
n n n 乘以 2 2 2 :n = n * 2
或 n = n << 1
n n n 除以 2 2 2 :n = n / 2
或 n = n >> 1
C/C++代码
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int cnt = 0;
while (n != 1)
{
if (n & 1) n = (3 * n + 1) >> 1;
else n = n >> 1;
cnt++;
}
cout << cnt << endl;
return 0;
}
边栏推荐
- Immutable
- JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
- SQL Analysis of ShardingSphere
- Pyspark机器学习:向量及其常用操作
- Pyspark Machine Learning: Vectors and Common Operations
- 【愚公系列】2022年07月 Go教学课程 024-函数
- 时时刻刻保持敬畏之心
- button remove black frame
- Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
- Mysql基础篇(Mysql数据类型)
猜你喜欢
Logitech Mouse Experience Record
typescript24-类型推论
typescript26 - literal types
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
typescript24 - type inference
ICML2022 | Deep Dive into Permutation-Sensitive Graph Neural Networks
The method of solving stored procedure table name passing through variable in mysql
Lawyer Interpretation | Guns or Roses?Talking about Metaverse Interoperability from the Battle of Big Manufacturers
【愚公系列】2022年07月 Go教学课程 025-递归函数
typescript23-tuple
随机推荐
Li Chi's work and life summary in July 2022
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
[kali-information collection] enumeration - DNS enumeration: DNSenum, fierce
lambda
【目标检测】YOLOv7理论简介+实践测试
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
在互联网时代,有诸多「互联网+」模式的诞生
C# | 使用Json序列化对象时忽略只读的属性
UE4 模型OnClick事件不生效的两种原因
TIM登陆时提示00001(TIM00001)
MySQL实践总结-
6-23漏洞利用-postgresql代码执行利用
剑指 Offer 68 - II. 二叉树的最近公共祖先
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
2022-07-31: Given a graph with n points and m directed edges, you can use magic to turn directed edges into undirected edges, such as directed edges from A to B, with a weight of 7.After casting the m
ModuleNotFoundError: No module named ‘tensorflow.keras‘报错信息的解决方法
MySQL-数据定义语言-DDLdatebase define language
7 行代码搞崩溃 B 站,原因令人唏嘘!
一个往年的朋友