繁体   English   中英

Java中long类型的给定正数的所有数字的总和

[英]Sum of all digits for a given Positive Number of type long in Java

我想知道为什么以下基于模运算的代码解决方案在从int类型转换为long类型时不起作用。

例如给定111111111111L我想返回12L

如何获得以下问题所述的相同预期行为(仅适用于int类型值)? 给定正数的所有数字之和

我也关注性能问题,因此我正在寻找有效的解决方案。

public static long sumTheDigitsVersion1(long inputValue){
    long sum = inputValue % 9L;
        if(sum == 0){
            if(inputValue > 0)
                return 9L;
        }
    return sum;
}

public static long sumTheDigitsVersion2(long inputValue){
    return inputValue - 9L * ((inputValue - 1L) / 9L);
}

谢谢

该解决方案不起作用,因为它是另一个问题的解决方案,即:

反复将数字的位数相加,直到获得一位数的结果。

换句话说,它计算111111111111 > 12 > 3

考虑一下, n % 9可能无法返回12 (这就是您所期望的)。

递归高效的解决方案:

public static long digitSum(long n) {
    if (n == 0)
        return 0;
    return n%10 + digitSum(n/10);
}

大约和您获得的效率一样:

private static final int PART_SIZE = 1000;
private static final int[] digitSums = new int[PART_SIZE];
static {
    for (int i = 0; i < digitSums.length; i++) {
        for (int n = i; n != 0; n /= 10) digitSums[i] += n % 10;
    }
}

public static long digitSum(long n) {
    int sum = 0;
    do {
        sum += digitSums[(int)(n % PART_SIZE)];
    } while ((n /= PART_SIZE) != 0);
    return sum;
}

这可能不是最有效的选择,但它是我想到的唯一一个选择:

public static long getDigitalSum(long n){
    n = Math.abs(n); //This is optional, remove if numbers are always positive. NOTE: Does not filter Long.MIN_VALUE

    char[] nums = String.valueOf(n).toCharArray();
    long sum = 0;

    for(char i:nums){
        sum = sum + Integer.parseInt(String.valueOf(i)); //Can use Long.parseLong() too
    }

    return sum;
}

经过一些不同数量的测试后,我得出了以下解决方案,比较了涉及3种不同方法的3种不同功能:

  • toCharArray()和循环,
  • 基本的数学计算和循环,
  • 递归。

我使用System.nanoTime()根据时间维度比较了三种不同的方法。

public static long sumTheDigits(long currentIterationValue){

    long currentDigitValue;
    long sumOutputValue = 0;

    while(currentIterationValue != 0) {
        currentDigitValue = currentIterationValue % 10;
        currentIterationValue = currentIterationValue / 10;
        sumOutputValue = sumOutputValue + currentDigitValue;
    }
    return sumOutputValue;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM