如何检查是否一个整数的3次方? [英] How to check if an integer is power of 3?

查看:120
本文介绍了如何检查是否一个整数的3次方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个问题,并弹出了这个想法。

解决方案

 同时(N%3 == 0){
    N / = 3;
}
返回否== 1;
 

请注意,1是三个零功耗。

编辑:您还需要在循环之前检查为零,作为循环不会终止对n = 0(感谢布鲁诺Rothgiesser)

I saw this question,and pop up this idea.

解决方案

while (n % 3 == 0) {
    n /= 3;
}
return n == 1;

Note that 1 is the zeroth power of three.

Edit: You also need to check for zero before the loop, as the loop will not terminate for n = 0 (thanks to Bruno Rothgiesser).

这篇关于如何检查是否一个整数的3次方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆