当前位置:网站首页>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)
    }
}
原网站

版权声明
本文为[易庞宙]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_29856589/article/details/80140895