【问题标题】:check sum calculator doesn't get the last digit校验和计算器没有得到最后一位数字
【发布时间】:2020-09-06 15:23:17
【问题描述】:

因此,当我输入一个 12 位长数字时,它会正确计算校验和。但如果我输入一个 13 位的长数字,它就会出错。

public static void checksum(long l) {

    long x1   = l / 100000000000l % 10;
    long x2   = l / 10000000000l % 10;
    long x3   = l / 1000000000 % 10;
    long x4   = l / 100000000 % 10;
    long x5   = l / 10000000 % 10;
    long x6   = l / 1000000 % 10;
    long x7   = l / 100000 % 10;
    long x8   = l / 10000 % 10;
    long x9   = l / 1000 % 10;
    long x10  = l / 100 % 10; 
    long x11  = l / 10 % 10;
    long x12  = l / 1 % 10;
    long x13  = l  % 10;
    
   long calculation = x1+(x2*3)+x3+(x4*3)+x5+(x6*3)+x7+(x8*3)+x9+(x10*3)+x11+(x12*3)+x13;
    System.out.println(calculation);
}

【问题讨论】:

  • 你能举出给定输入、输出和预期输出的例子吗?并分享你如何调用方法的代码,错误也可能在那里
  • 当输入为 3729483022008 时,预期输出为 100。我得到的是 107
  • 你打电话给3729483022008还是3729483022008L,结果是91
  • l / 1 % 10l % 10 相同。
  • 我打电话给 3729483022008L

标签: java math operators


【解决方案1】:

这是您需要使用的。在最初的模块化计算中,您的误差是 10 倍。

checksum(3729483022008L);
    
    
public static void checksum(long l) {
        
        long x1   = l / 1000000000000L % 10;
        long x2   = l / 100000000000L % 10;
        long x3   = l / 10000000000L % 10;
        long x4   = l / 1000000000 % 10;
        long x5   = l / 100000000 % 10;
        long x6   = l / 10000000 % 10;
        long x7   = l / 1000000 % 10;
        long x8   = l / 100000 % 10;
        long x9   = l / 10000 % 10;
        long x10  = l / 1000 % 10; 
        long x11  = l / 100 % 10;
        long x12  = l / 10 % 10;
        long x13  = l / 1 % 10;
        
        long calculation = x1 + (x2 * 3) + x3 + (x4 * 3) + x5
                + (x6 * 3) + x7 + (x8 * 3) + x9 + (x10 * 3) + x11
                + (x12 * 3) + x13;
        
        System.out.println(calculation);
}

打印

100

【讨论】:

    【解决方案2】:

    这是正常的,因为要获得第一个数字,x1 你除以10^11,然后你应用%10,数字大于 10^13(所以 13-len 或更多)它不要使用新的数字

    System.out.println(l / 100000000000L);      // 37
    System.out.println(l / 100000000000L % 10); // 7
    

    操作number / ((long) Math.pow(10, i)) % 10numberi-th 数字,所以以12 为最大,你不会得到第13 或更大的数字


    事实上,这个问题与你使用最后一个 8 两次的另一个问题有关,你需要移动所有因子,将它们乘以 10

    public static void checkSum(long l) {
    
        long x1 = l / 1000000000000L % 10;
        long x2 = l / 100000000000L % 10;
        long x3 = l / 10000000000L % 10;
        long x4 = l / 1000000000 % 10;
        long x5 = l / 100000000 % 10;
        long x6 = l / 10000000 % 10;
        long x7 = l / 1000000 % 10;
        long x8 = l / 100000 % 10;
        long x9 = l / 10000 % 10;
        long x10 = l / 1000 % 10;
        long x11 = l / 100 % 10;
        long x12 = l / 10 % 10;
        long x13 = l % 10;
    
        long calculation = x1 + (x2 * 3) + x3 + (x4 * 3) + x5 + (x6 * 3) + x7 + (x8 * 3) + x9 +
            (x10 * 3) + x11 + (x12 * 3) + x13;
        System.out.println(calculation);
    
    }
    

    【讨论】:

    • @sanandereas35 是为了什么?使用第 12 位之后的所有数字?删除 %10``
    • 如果我在 x2 和 x1 之后删除 %10(假设这就是你的意思),我会得到 1247 作为输出
    • @sanandereas35 不只是 x1,你如何得到 100 ?小心你使用最后一个8 两次,在 x12 和 x13 中
    • @sanandereas35 刚刚知道吗?您需要通过将 0 添加到所有来移动所有,以获得第一个数字,而不是 8 两次,看看我的编辑
    • 对不起,预期的输出是 92。如果我从 x1 中删除 %10,我得到:137。我找到了这个德国网站,您可以在其中输入一个数字并计算其校验和,但具有不同的因素:1*3+7*3+1*2.... :rechneronline.de/quersumme/gewichtet.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2015-04-14
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多