给定一个五位数的数字N。任务是在将数字修改为以下数字后找到给定数字的后五位数提高到5的幂:
first digit, third digit, fifth digit, fourth digit, second digit.
例子:
Input : N = 12345
Output : 71232
Explanation :
After modification the number becomes 13542. (13542)5 is
455422043125550171232
Input : N = 10000
Output : 00000
方法:在此问题中,仅需要执行语句中描述的动作。但是,此问题有两个陷阱。
第一个陷阱是不能用64位整数表示五位数的第五次幂。但是我们实际上并不需要第五幂,我们需要第五幂以10 5为模。每次乘法后都可以应用mod运算。
第二个陷阱是您需要输出五个数字,而不是第五个幂模10 5 。区别在于从末尾开始的第五个数字为零。要输出,前导零的数字可以使用相应的格式(printf中的%05d),也可以提取数字并一一输出。
下面是上述方法的实现:
C++
// CPP program to find last five digits
// of a five digit number raised to power five
#include
using namespace std;
// Function to find the last five digits
// of a five digit number raised to power five
int lastFiveDigits(int n)
{
n = (n / 10000) * 10000
+ ((n / 100) % 10)
* 1000
+ (n % 10)
* 100
+ ((n / 10) % 10)
* 10
+ (n / 1000) % 10;
int ans = 1;
for (int i = 0; i < 5; i++) {
ans *= n;
ans %= 100000;
}
printf("%05d", ans);
}
// Driver code
int main()
{
int n = 12345;
lastFiveDigits(n);
return 0;
}
Java
// Java program to find last five digits
// of a five digit number raised to power five
class GfG {
// Function to find the last five digits
// of a five digit number raised to power five
static void lastFiveDigits(int n)
{
n = (n / 10000) * 10000
+ ((n / 100) % 10)
* 1000
+ (n % 10)
* 100
+ ((n / 10) % 10)
* 10
+ (n / 1000) % 10;
int ans = 1;
for (int i = 0; i < 5; i++) {
ans *= n;
ans %= 100000;
}
System.out.println(ans);
}
// Driver code
public static void main(String[] args)
{
int n = 12345;
lastFiveDigits(n);
}
}
Python3
# Python3 program to find last five digits
# of a five digit number raised to power five
# Function to find the last five digits
# of a five digit number raised to power five
def lastFiveDigits(n):
n = ((int)(n / 10000) * 10000 +
((int)(n / 100) % 10) * 1000 + (n % 10) * 100 +
((int)(n / 10) % 10) * 10 + (int)(n / 1000) % 10)
ans = 1
for i in range(5):
ans *= n
ans %= 100000
print(ans)
# Driver code
if __name__ == '__main__':
n = 12345
lastFiveDigits(n)
# This code contributed by PrinciRaj1992
C#
// C# program to find last five
// digits of a five digit number
// raised to power five
using System;
class GFG
{
// Function to find the last
// five digits of a five digit
// number raised to power five
public static void lastFiveDigits(int n)
{
n = (n / 10000) * 10000 +
((n / 100) % 10) * 1000 +
(n % 10) * 100 +
((n / 10) % 10) * 10 +
(n / 1000) % 10;
int ans = 1;
for (int i = 0; i < 5; i++)
{
ans *= n;
ans %= 100000;
}
Console.WriteLine(ans);
}
// Driver code
public static void Main(string[] args)
{
int n = 12345;
lastFiveDigits(n);
}
}
// This code is contributed
// by Shrikant13
PHP
Javascript
输出:
71232