โจทย์
ได้รับจำนวน N (5 หลัก) ทางเราต้อง หาตัวเลข 5 หลักสุดท้ายของ N^5
ตัวอย่าง
Input: N = 25211
Output: (ตัวอย่างโค้ดใช้ 12345 ได้ 65625)
แนวทางแก้ปัญหา
ต้องการเฉพาะ 5 หลักสุดท้าย ใช้ Modulo 100000 หลังการคูณทุกครั้ง:
- เริ่ม result = 1
- วน 5 รอบ: result = (result * N) % 100000
โปรแกรมตัวอย่าง
#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