当前位置:网站首页>Kotlin算法入门求回文数数算法优化二数字生成规则
Kotlin算法入门求回文数数算法优化二数字生成规则
2022-08-11 08:01:00 【易庞宙】
class PalindromeNumber2 { /*生成一位数字的回文数*/ private fun adigit(parentNumber: Long, tenID: Long) { val tenIDResult = Math.pow(10.0, tenID.toDouble()).toLong() for (i in 0..9) { println(parentNumber + i * tenIDResult) } } /*生成两位数字*/ private fun twodigit(parentNumber: Long, tenID: Long, iszero: Boolean) { var i = 0 if (iszero) i = 0 else i = 1 val tenIDResult = Math.pow(10.0, tenID.toDouble()).toLong() while (i <= 9) { println(parentNumber + 11 * i.toLong() * tenIDResult) i++ } } /*生成三位数字*/ private fun threedigit(parentNumber: Long, tenLID: Long, isThree: Boolean, tenID: Long) { var i: Long = 1 if (!isThree) i = 0 while (i <= 9) { var result = parentNumber if (isThree) { adigit((i * Math.pow(10.0, 2.0) + i).toLong(), tenID) } else { result += (i * Math.pow(10.0, (2 * tenID - tenLID).toDouble()) + i * Math.pow(10.0, tenLID.toDouble())).toLong() // System.out.println("3位数"+i+"p...."+result+"i>>>>>"+i); adigit(result, tenID) } ++i } } /*生成四位数字位数字*/ private fun fourdigit(parentNumber: Long, tenFID: Long, tenLID: Long, isfour: Boolean, tenID: Long) { var i: Long = 1 if (!isfour) i = 0 while (i <= 9) { if (isfour) { twodigit((i * Math.pow(10.0, 3.0) + i).toLong(), tenID, true) } else { twodigit((i * Math.pow(10.0, tenFID.toDouble()) + i * Math.pow(10.0, tenLID.toDouble()) + parentNumber.toDouble()).toLong(), tenID - 1, true) } i++ } } /*指定从个位数字生成到n位数字间的回文数字打印*/ fun PalindromeNumber(n: Long) { if (n <= 0) return for (i in 1..n) { if (i == 1L) { adigit(0, 0) continue } else if (i == 2L) { twodigit(0, 0, false) continue } else if (i == 3L) { threedigit(0, 0, true, 1) continue } else if (i == 4L) { fourdigit(0, 0, 0, true, 1) continue } oddDigitPalindromeNumber(i, 0, false, 0, 0) } } fun oddDigitPalindromeNumber(n: Long, tenId: Long, isZero: Boolean, PN: Long, lastTenId: Long) { var tenId = tenId var PN = PN var lastTenId = lastTenId var i = 0 /*判断开头是否是0*/ if (!isZero) { tenId = n / 2 i = 1 } /*大于及等于5以上奇数位回文数生成方法*/ if (n == 3L) { // System.out.println("nT"+nextTenId); threedigit(PN, lastTenId, false, tenId) return } else if (n == 4L) { // System.out.println("lId:::"+(2*tenId-lastTenId-1)); fourdigit(PN, 2 * tenId - lastTenId - 1, lastTenId, false, tenId) return }/*大于及等于6以上偶数位回文数生成方法*/ while (i <= 9) { var result = PN if (!isZero) { PN = (i * Math.pow(10.0, (n - 1).toDouble()) + i).toLong() result = PN lastTenId = 0 } else { result += (i * Math.pow(10.0, (2 * tenId - lastTenId).toDouble()) + i * Math.pow(10.0, lastTenId.toDouble())).toLong() // System.out.println("res"+result); } //if (!isZero) System.out.println("i"+i); ++i oddDigitPalindromeNumber(n - 2, tenId, true, result, lastTenId + 1) } } //测试单独生成N为的回文数字方法 fun fire() { oddDigitPalindromeNumber(6, 0, false, 0, 0) } }
边栏推荐
猜你喜欢
1076 Wifi密码 (15 分)
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
Find the latest staff salary and the last staff salary changes
少年成就黑客,需要这些技能
关于架构的认知
Decrement operation in tf; tf.assign_sub()
1.2 - error sources
1002 Write the number (20 points)
The growth path of a 40W test engineer with an annual salary, which stage are you in?
【LeetCode】链表题解汇总
随机推荐
3.1-分类-概率生成模型
go 操作MySQL之mysql包
1106 2019数列 (15 分)
1002 Write the number (20 points)
1046 划拳 (15 分)
leetcode: 69. Square root of x
零基础SQL教程: 主键、外键和索引 04
零基础SQL教程: 基础查询 05
Break pad source code compilation--refer to the summary of the big blogger
美术2.4 UV原理基础
抽象类和接口
Item 2 - Annual Income Judgment
2022 China Soft Drink Market Insights
Find the latest staff salary and the last staff salary changes
【云原生】云原生在网络安全领域的应用
【415. 字符串相加】
[C语言] sscanf如何实现sscanf_s?
9、Neural Sparse Voxel Fields
tf.reduce_mean() and tf.reduce_sum()
用 Antlr 重构脚本解释器