博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3的次幂
阅读量:4181 次
发布时间:2019-05-26

本文共 1184 字,大约阅读时间需要 3 分钟。

package com.xiaobu.leetcode;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/10/23 16:51 * @description 判断一个整数是否为3的次幂 还可以用循环、递归实现 */public class CheckPowerOfThree {
public static boolean checkIsPowerOfThreeByEazyWay(int n) {
return n > 0 && Math.pow(3, 19) % n == 0; } public static void main(String[] args) {
boolean flag = checkIsPowerOfThreeByEazyWay(1); System.out.println("flag = " + flag); System.out.println("Integer.MAX_VALUE="+Integer.MAX_VALUE); //3的20次幂 long threePowOfNineteen = (long) Math.pow(3, 19); System.out.println("threePowOfNineteen = " + threePowOfNineteen); long threePowOfTwenty = (long) Math.pow(3, 20); System.out.println("threePowOfTwenty = " + threePowOfTwenty); boolean result = checkIsPowerOfThreeByRecursive(81); System.out.println("result = " + result); } /** * 递归法 */ public static boolean checkIsPowerOfThreeByRecursive(int n) {
if (n == 1) {
return true; } if (n == 0) {
return false; } return checkIsPowerOfThreeByRecursive(n / 3) && n % 3 == 0; }}

转载地址:http://zzgai.baihongyu.com/

你可能感兴趣的文章
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>
源码编译安装LNMP环境之Nginx篇
查看>>
源码编译安装LNMP环境之PHP篇
查看>>
Linux中rpm工具使用教程
查看>>
Linux中yum工具使用教程
查看>>
C++字符串函数
查看>>
mknod详解
查看>>
linux中的run-level何解?
查看>>
Linux内核编译详解(转自linuxSir)
查看>>
实模式,保护模式与V86模式
查看>>