PAT 乙级 1001 害死人不偿命的(3n+1)猜想 (15分) Java

这篇博客介绍了卡拉兹猜想,一个关于正整数的数学问题,该猜想表明任何正整数通过特定运算最终都会达到1。博主给出了一个简单的Java程序,用于计算给定正整数达到1所需的步数,以此验证猜想的过程。程序读取输入,根据奇偶性进行运算,并输出计算步数。

题目链接

https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528

描述

卡拉兹(Callatz)猜想:

对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一
步得到 n=1。卡拉兹在 1950 年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命
题,结果闹得学生们无心学业,一心只证 (3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……

我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过 1000 的正整数 n,简单地数一下,需要多少步(砍几下)才能得到 n=1

输入格式

每个测试输入包含 1 个测试用例,即给出正整数 n 的值。

输出格式

输出从 n 计算到 1 需要的步数。

输入样例

3

输出样例

5

代码

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        int n = input.nextInt();
        int res = 0;
        
        while (n != 1) {
            n = n % 2 == 1 ? (3 * n + 1) / 2 : n / 2;
            res++;
        }
        
        System.out.println(res);
        
        input.close();
    }
}
Java谜题 Java谜题1——表达式谜题 谜题1:奇数性 谜题2:找零时刻 谜题3:长整除 谜题4:初级问题 谜题5:十六进制的趣事 谜题6:多重转型 谜题7:互换内容 谜题8:Dos Equis 谜题9:半斤 谜题10:八两 Java谜题2——字符谜题 谜题11:最后的笑声 谜题12:ABC 谜题13:畜牧场 谜题14:转义字符的溃败 谜题15:令晕头转向的Hello 谜题16:行打印程序 谜题17:嗯? 谜题18:字符串奶酪 谜题19:漂亮的火花(块注释符) 谜题20:我的类是什么? 谜题21:我的类是什么?第2幕 谜题22:URL的愚弄 谜题23劳无获 Java谜题3——循环谜题 谜题24:尽情享受每一个字节 谜题25:无情的增量操作 谜题26:在循环中 谜题27:变幻莫测的i值 谜题28:循环者 谜题29:循环者的新娘 谜题30:循环者的爱子 谜题31:循环者的鬼魂 谜题32:循环者的诅咒 谜题33:循环者遇到了狼 谜题34:被计数击倒了 谜题35:一钟又一Java谜题4——异常谜题 谜题36:优柔寡断 谜题37:极端可思议 谜题38:受欢迎的宾客 谜题39:您好,再见 谜题40:情愿的构造器 谜题41:域和流 谜题42:异常为循环而抛 谜题43:异常地危险 谜题44:切掉类 谜题45:令疲惫堪的测验 Java谜题5——类谜题 谜题46:令混淆的构造器案例 谜题47:啊呀!我的猫变成狗了 谜题48:我所得到的都是静态的 谜题49:比生命更大 谜题50:是你的类型 谜题51:那个点是什么? 谜题52:合计数的玩笑 谜题53:按你的意愿行事 谜题54:Null与Void 谜题55:特创论 Java谜题6——库谜题 谜题56:大问题 谜题57:名字里有什么? 谜题58:产生它的散列码 谜题59:什么是差? 谜题60:一行的方法 谜题61:日期游戏 谜题62:名字游戏 谜题63:更多同样的问题 谜题64:按余数编组 谜题65:一种疑似排序的惊传奇 Java谜题7——更多的类谜题 谜题66:一件私事 谜题67:对字符串上瘾 谜题68:灰色的阴影 谜题69:黑色的渐隐 谜题70:一揽子交易 谜题71:进口税 谜题72:终极危难 谜题73:你的隐私正在公开 谜题74:同一性的危机 谜题75:头还是尾? 名字重用的术语表 Java谜题8——更多的库谜题 谜题76:乒乓 谜题77:搞乱锁的妖怪 谜题78:反射的污染 谜题79:这是狗的生活 谜题80:更深层的反射 谜题81:烧焦(字符化)到无法识别 谜题82:啤酒爆炸 谜题83:诵读困难者的一神论 谜题84:被粗暴地中断 谜题85:惰性初始化 Java谜题9——高级谜题 谜题86:有毒的括号垃圾 谜题87:紧张的关系 谜题88:原生类型的处理 谜题89:泛型迷药 谜题90:荒谬痛苦的超类 谜题91:序列杀手 谜题92:双绞线 谜题93:类的战争 谜题94:迷失在混乱中 谜题95:只是些甜点 ********************* Chapter 1. Introduction Chapter 2. Expressive Puzzlers Puzzle 1: Oddity Puzzle 2: Time for a Change Puzzle 3: Long Division Puzzle 4: It's Elementary Puzzle 5: The Joy of Hex Puzzle 6: Multicast Puzzle 7: Swap Meat Puzzle 8: Dos Equis Puzzle 9: Tweedledum Puzzle 10: Tweedledee Chapter 3. Puzzlers with Character Puzzle 11: The Last Laugh Puzzle 12: ABC Puzzle 13: Animal Farm Puzzle 14: Escape Rout Puzzle 15: Hello Whirled Puzzle 16: Line Printer Puzzle 17: Huh? Puzzle 18: String Cheese Puzzle 19: Classy Fire Puzzle 20: What's My Class? Puzzle 21: What's My Class, Take 2 Puzzle 22: Dupe of URL Puzzle 23: No Pain, No Gain Chapter 4. Loopy Puzzlers Puzzle 24: A Big Delight in Every Byte Puzzle 25: Inclement Increment Puzzle 26: In the Loop Puzzle 27: Shifty i's Puzzle 28: Looper Puzzle 29: Bride of Looper Puzzle 30: Son of Looper Puzzle 31: Ghost of Looper Puzzle 32: Curse of Looper Puzzle 33: Looper Meets the Wolfman Puzzle 34: Down for the Count Puzzle 35: Minute by Minute Chapter 5. Exceptional Puzzlers Puzzle 36: Indecision Puzzle 37: Exceptionally Arcane Puzzle 38: The Unwelcome Guest Puzzle 39: Hello, Goodbye Puzzle 40: The Reluctant Constructor Puzzle 41: Field and Stream Puzzle 42: Thrown for a Loop Puzzle 43: Exceptionally Unsafe Puzzle 44: Cutting Class Puzzle 45: Exhausting Workout Chapter 6. Classy Puzzlers Puzzle 46: The Case of the Confusing Constructor Puzzle 47: Well, Dog My Cats! Puzzle 48: All I Get Is Static Puzzle 49: Larger Than Life Puzzle 50: Not Your Type Puzzle 51: What's the Point? Puzzle 52: Sum Fun Puzzle 53: Do Your Thing Puzzle 54: Null and Void Puzzle 55: Creationism Chapter 7. Library Puzzlers Puzzle 56: Big Problem Puzzle 57: What's in a Name? Puzzle 58: Making a Hash of It Puzzle 59: What's the Difference? Puzzle 60: One-Liners Puzzle 61: The Dating Game Puzzle 62: The Name Game Puzzle 63: More of the Same Puzzle 64: The Mod Squad Puzzle 65: A Strange Saga of a Suspicious Sort Chapter 8. Classier Puzzlers Puzzle 66: A Private Matter Puzzle 67: All Strung Out Puzzle 68: Shades of Gray Puzzle 69: Fade to Black Puzzle 70: Package Deal Puzzle 71: Import Duty Puzzle 72: Final Jeopardy Puzzle 73: Your Privates Are Showing Puzzle 74: Identity Crisis Puzzle 75: Heads or Tails? A Glossary of Name Reuse Chapter 9. More Library Puzzlers Puzzle 76: Ping Pong Puzzle 77: The Lock Mess Monster Puzzle 78: Reflection Infection Puzzle 79: It's a Dog's Life Puzzle 80: Further Reflection Puzzle 81: Charred Beyond Recognition Puzzle 82: Beer Blast Puzzle 83: Dyslexic Monotheism Puzzle 84: Rudely Interrupted Puzzle 85: Lazy Initialization Chapter 10. Advanced Puzzlers Puzzle 86: Poison-Paren Litter Puzzle 87: Strained Relations Puzzle 88: Raw Deal Puzzle 89: Generic Drugs Puzzle 90: It's Absurd, It's a Pain, It's Superclass! Puzzle 91: Serial Killer Puzzle 92: Twisted Pair Puzzle 93: Class Warfare Puzzle 94: Lost in the Shuffle Puzzle 95: Just Desserts Appendix A. Catalog of Traps and Pitfalls Section 1. Lexical Issues Section 2. Integer Arithmetic Section 3. Floating-Point Arithmetic Section 4. Expression Evaluation Section 5. Flow of Control Section 6. Class Initialization Section 7. Instance Creation and Destruction Section 8. Other Class- and Instance-Related Topics Section 9. Name Reuse Section 10. Strings Section 11. I/O Section 12. Threads Section 13. Reflection Section 14. Serialization Section 15. Other Libraries Appendix B. Notes on the Illusions Ambiguous Figures Impossible Figures Geometrical Illusions: Size Geometrical Illusions: Direction Subjective Contours Anomalous Motion Illusions Illusions of Lightness Compound Illusions
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值