当前位置:网站首页>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) } }
边栏推荐
猜你喜欢
随机推荐
高德能力API
1061 True or False (15 points)
C Primer Plus(6) 中文版 第1章 初识C语言 1.1 C语言的起源 1.2 选择C语言的理由 1.3 C语言的应用范围
matplotlib
2022 China Soft Drink Market Insights
1002 写出这个数 (20 分)
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
机器学习(一)数据的预处理
go 操作MySQL之mysql包
分布式锁-Redission - 缓存一致性解决
1056 Sum of Combinations (15 points)
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
4.1 - Support Vector Machines
1081 检查密码 (15 分)
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子
【415. 字符串相加】
1091 N-自守数 (15 分)
1076 Wifi密码 (15 分)
XXL-JOB 分布式任务调度中心搭建
Serverless + domain name can also build a personal blog? Really, and soon