如何解决校验和计算器未获得最后一位数字
因此,当我输入一个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);
}
解决方法
这是您需要使用的。您的初始模块化计算量减少了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
,
那是正常的,因为要得到第一个数字,用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)) % 10
取i-th
的{{1}}位,因此以12为最大位,您将得到第13位或更大的数字
实际上,这个问题伴随着您上次使用number
两次的情况,您需要移动所有因子,并将其乘以10
8
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。