校验和计算器未获得最后一位数字。

huangapple go评论82阅读模式
英文:

check sum calculator doesn't get the last digit

问题

以下是翻译好的代码部分:

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);
}
英文:

So when I put in a 12-digit long number it calculates the check sum correctly. But if I put in a 13-digit long number it does something wrong.

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);
}

答案1

得分: 1

这是你需要使用的内容。在你最初的模块计算中,存在一个10倍的误差。

checksum(3729483022008L);

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 calculation = x1 + (x2 * 3) + x3 + (x4 * 3) + x5
            + (x6 * 3) + x7 + (x8 * 3) + x9 + (x10 * 3) + x11
            + (x12 * 3);

    System.out.println(calculation);
}

输出结果为

100
英文:

Here is what you need to use. You were off by a factor of 10 in your initial modular computations.

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);
}

Prints

100

</details>



# 答案2
**得分**: 0

以下是翻译后的内容:

那是正常的,因为要获取第一个数字,使用 `x1` 除以 `10^11`,然后应用 `%10`,使用大于 10^13(因此长度为 13 或更多位)的数字不会使用新的数字。

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

操作 number / ((long) Math.pow(10, i)) % 10 获取了 number 的第 i 位数字,因此最大为 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);

}
英文:

That is normal because the to get the first digit, with x1 you divide by 10^11, but then you apply %10, with a number bigger that 10^13 (so 13-len or more) it do not uses the new digits

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

The operation number / ((long) Math.pow(10, i)) % 10 takes the i-th digit of number, so with 12 as the biggest one, you won't get the 13th or bigger


In fact this problem goes along the other that you used the last 8 twice, you need to shift all factors, multiply them by 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);

}

huangapple
  • 本文由 发表于 2020年9月6日 23:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63765768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定