当前位置:网站首页>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) } }
边栏推荐
猜你喜欢
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
1091 N-自守数 (15 分)
8、Mip-NeRF
1.2 - error sources
Creo9.0 特征的成组
1061 True or False (15 points)
3.2 - classification - Logistic regression
租房小程序
用 Antlr 重构脚本解释器
随机推荐
【Day_13 0509】▲跳石板
1046 punches (15 points)
流式结构化数据计算语言的进化与新选择
Break pad source code compilation--refer to the summary of the big blogger
JRS303-数据校验
迷你图书馆系统(对象+数组)
4.1 - Support Vector Machines
【415. 字符串相加】
4.1-支持向量机
Keep track of your monthly income and expenses through bookkeeping
支持各种文件快速重命名最简单的小技巧
抽象类和接口
选择收银系统主要看哪些方面?
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
8、Mip-NeRF
【实战系列】OpenApi设计规范
Find the latest staff salary and the last staff salary changes
There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
机器学习(三)多项式回归