2015 Huawei server test-the ending number of the high power, and the ending number of the 2015 Huawei power
Description:
The last three digits (M, N are all greater than 10) of the N power of M ).
Interface Description
Prototype:
Int GetLast3DigitsOfMN (int M, int N );
Input parameters:
Int M: M> 10
Int N: N> 10
Return Value:
The last three digits of M's n power
Solution:
Idea 1: first calculate the Npower of M and then M ^ N % 1000 calculate the last three digits. However, the Npower of M is likely to overflow, resulting in an exception or error in the output result.
Train of Thought 2: If train of thought fails, we need to find its corresponding equivalent method. M calculates the remainder for every multiplication of M, and its last three digits are equivalent.
The Code is as follows:
Public static int getLast3DigitsOfMN (int M, int N) {int temp = M; for (int I = 1; I <N; I ++) {temp = M * temp % 1000;} return temp ;}}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.