我有10位数的长数字,我现在想要实现此检查的最佳方法。我将用一个例子来解释它:
如果我们的号码是3456789123:
3 will be multiplied by 10.
4 will be multiplied by 9.
5 will be multiplied by 8.
6 will be multiplied by 7.
...
2 will be multiplied by 2.
The last 3 will be multiplied by 1.
因此,将返回此操作的结果:
(3*10) + (4*9) + ... + (2*2) + (1*1)
这很简单,直接创建,阵列和乘法,但我试图找到最佳解决方案。
任何提示?
由于
答案 0 :(得分:3)
for (int i = 0; i < 10; i++) {
long curr = x % 10 * (i+1);
x = x / 10;
System.out.println(curr);
//do something with curr
}
重复除以10并取最右边的数字,并将其乘以当前权重的迭代器。
答案 1 :(得分:2)
这可能会对你有所帮助
long weight=1;
long finalSum=0;
while(number>0){
long a=number%10;
finalSum+=(a*weight);
weight++;
number/=10;
}
if((finalSum%11)==10){
System.out.println("Final sum when divided by 11 gives remainder 10");
}