检查数字是否是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 婆婆高寿祝福语大全简短