检查数字是否是C ++中另一个数字的幂
在这里,我们将看到一个数字是否是另一个数字的幂。假设数字为125,另一个数字为5。因此,当发现125为5的幂时,它将返回true。在这种情况下,它为true。125=53。
算法
isRepresentPower(x, y):
Begin
if x = 1, then
if y = 1, return true, otherwise false
pow := 1
while pow < y, do
pow := pow * x
done
if pow = y, then
return true
return false
End示例
#include<iostream>
#include<cmath>
using namespace std;
bool isRepresentPower(int x, int y) {
if (x == 1)
return (y == 1);
long int pow = 1;
while (pow < y)
pow *= x;
if(pow == y)
return true;
return false;
}
int main() {
int x = 5, y = 125;
cout << (isRepresentPower(x, y) ? "Can be represented" : "Cannot be represented");
}输出结果
Can be represented
热门推荐
10 新婚祝福语大全简短精炼
11 父亲祝福语大全简短语
12 亲戚生日红包祝福语简短
13 爱情祝福语大全 简短实际
14 对生活祝福语简短精辟
15 祝福语怎么写大全简短
16 简短霸气的考试祝福语
17 过节文案成语祝福语简短
18 女儿结婚账单祝福语简短