ตรวจสอบว่าเลขสามารถเขียนในรูป aᵇ ได้หรือไม่ (ซ้ำอีกครั้ง)
เนื้อหาเหมือน ID 1983 และ 1984 ทุกประการ
ตัวอย่างโค้ด
#include<iostream>
#include<cmath>
using namespace std;
bool isRepresentPower(int num) {
if (num == 1) return true;
for (int i = 2; i * i <= num; i++) {
double val = log(num) / log(i);
if ((val - (int)val) < 0.00000001) return true;
}
return false;
}
int main() {
int n = 125;
cout << (isRepresentPower(n) ? "Can be represented" : "Cannot be represented");
}
ผลลัพธ์
Can be represented