数字从右边开始的第二个数字

时间:2021-10-07 17:54:49

Now I actually read about modular arithmetic and managed to get the last digit of some number. Okay. But now... How do I get second last digit? Second one on the right. I actually been working on it on several hours straight trying to find a simple solution, this is the best I could come up so far but it's still not it. Could somebody help me please?

现在我实际上阅读了模数运算,并设法得到一些数字的最后一位数。好的。但是现在......我怎么得到倒数第二个数字?第二个在右边。我实际上是在几个小时之后一直在努力寻找一个简单的解决方案,这是迄今为止我能提出的最好的但它仍然不是它。有人可以帮帮我吗?

This is what I have so far

这就是我到目前为止所拥有的

long long powmod(long long n, long long exp)
{
    long long r, result = 1;

    while(exp)
    {
        r = exp % 2;
        exp /= 2;
        if(r == 1) result = result * n % 10;
        n = (n * n) % 10;
    }
    return result;
}

Thanks in advance

提前致谢

2 个解决方案

#1


1  

As you only asked for the second last digit, how about getting the last two digits, then dividing by ten?

因为你只要求倒数第二个数字,如何获得最后两位数,然后除以10?

Ie, solve for a^n mod 100, then look at the tens digit.

即,求解^ n mod 100,然后查看十位数。

#2


3  

Divide it by ten, round down, and then get the last digit of what remains. :-)

将它除以10,向下舍入,然后得到剩下的最后一位数。 :-)

#1


1  

As you only asked for the second last digit, how about getting the last two digits, then dividing by ten?

因为你只要求倒数第二个数字,如何获得最后两位数,然后除以10?

Ie, solve for a^n mod 100, then look at the tens digit.

即,求解^ n mod 100,然后查看十位数。

#2


3  

Divide it by ten, round down, and then get the last digit of what remains. :-)

将它除以10,向下舍入,然后得到剩下的最后一位数。 :-)