在 C++ 中查詢給定五位數乘方末尾五位數字


此題中,我們給出數字 N。我們的任務是找出給定五位數乘方後,末尾五位數字。 

讓我們舉個例子來理解此問題, 

輸入: N = 25211

輸出

解決方案方法

為了解決此問題,我們只需要找出所得值的後五位數字。因此,我們將透過查詢數字的五位數餘數,來求出每次冪次增加後數字的最後一位。 最後返回 5 次冪後的末尾五位數。

展示我們解決方案工作原理的程式,

示例

即時演示

#include <iostream>
using namespace std;

int lastFiveDigits(int n) {
   int result = 1;
   for (int i = 0; i < 5; i++) {
      result *= n;
      result %= 100000;
   }
   cout<<"The last five digits of "<<n<<" raised to the power 5 are "<<result;
}

int main() {
   int n = 12345;
   lastFiveDigits(n);
   return 0;
}

輸出

The last five digits of 12345 raised to the power 5 are 65625

更新於: 2021 年 1 月 25 日

217 次瀏覽

開啟你的職業生涯

完成本課程獲取認證

開始
廣告