当前位置:网站首页>整數的分劃問題
整數的分劃問題
2022-07-20 08:07:00 【zoyation】
整數的分劃問題。
如,對於正整數n=6,可以分劃為:
6
5+1
4+2, 4+1+1
3+3, 3+2+1, 3+1+1+1
2+2+2, 2+2+1+1, 2+1+1+1+1
1+1+1+1+1+1+1
現在的問題是,對於給定的正整數n,編寫算法打印所有劃分。
用戶從鍵盤輸入 n (範圍1~10)
程序輸出該整數的所有劃分。
今天花了差不多一天的時間用java把這個整數劃分問題解决了,思路很清晰,邏輯卻有點複雜哦
基本思路是:使用棧結構,從後邊向前邊推,把每一次分解出的數放到一個動態數組ArrayList中,然後根據動態數組中的數產生一個輸出字符串,並放到棧裏面,當然,逗號,換行也在適當時候放到棧裏,然後就是輸出了,說也沒怎麼說清楚,就看代碼吧,看了可能會更糊塗,不過花點時間還是可以理解的
程序運行效果如下:
請輸入你要劃分的整數:60
請輸入【1,50】之間的整數:50
請輸入你要劃分的整數:10
10
9+1
8+2,8+1+1
7+3,7+2+1,7+1+1+1
6+4,6+3+1,6+2+2,6+2+1+1,6+1+1+1+1
5+5,5+4+1,5+3+2,5+2+2+1,5+2+1+1+1,5+1+1+1+1+1
4+4+2,4+3+3,4+3+2+1,4+2+2+2,4+2+2+1+1,4+2+1+1+1+1,4+1+1+1+1+1+1
3+3+2+2,3+2+2+2+1,3+2+2+1+1+1,3+2+1+1+1+1+1,3+1+1+1+1+1+1+1
2+2+2+2+2,2+2+2+2+1+1,2+2+2+1+1+1+1,2+2+1+1+1+1+1+1,2+1+1+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1+1
請輸入你要劃分的整數:
边栏推荐
猜你喜欢
Spiderpi convenient operation manual
Next time, the interviewer will ask about the design of high concurrency system and directly dump this article to him
PC网站实现微信扫码登录功能(二)
Flask framework - model relationships (many to many)
What are the main aspects of interface testing? What skills are needed? How to learn?
四剑客与Code Review的恩怨情仇:“始乱终弃”到“浪子回头”
Rlib learning [2] --env definition + env rollout
超超超超写实的数字人!让你24小时不停播
Mysql 面向企业级开发(一)
cmd执行命令出现SecurityError: (:) [],ParentContainsErrorRecordException
随机推荐
BERT-tutorial
SQL 时间拼接问题,系统自动截断的拼接复原
Leetcode 199 Right view of binary tree (2022.07.18)
BERT-tutorial
提交时显示找不到匹配的主机密钥类型。
Etcd database source code analysis -- etcdserver run apply process
单实例Mongo升级为副本集
Summary of interview questions (4) TCP / IP four-layer model, three handshakes and four waves, one more and one less. No, the implementation principle of NiO
CSimpleArray
Basic knowledge about coding table
Research Report on research and development prospects of China's strontium carbonate industry (2022 Edition)
Flask framework - model relationships (many to many)
PC网站实现微信扫码登录功能(二)
输入文本自动生成图像,太好玩了!
Firewall port forwarding
面试题总结(4) Tcp / Ip 四层模型,三次握手四次挥手,多一次少一次可以不,NIO的实现原理
无重叠区间[贪心练习]
JS 99 multiplication table
C语言编程题:(C语言)分糖果 通俗易懂
Improvement 21 of yolov5: cnn+transformer - replace the backbone network with fast and strong lightweight backbone efficientformer