码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 326

时间:2016-05-10 20:30:51      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

Power of Three

Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?

 

 1 /*************************************************************************
 2     > File Name: LeetCode326.c
 3     > Author: Juntaran    
 4     > Mail: Jacinthmail@gmail.com
 5     > Created Time: 2016年05月10日 星期二 02时42分09秒
 6  ************************************************************************/
 7 
 8 /*************************************************************************
 9     
10     Power of Three
11     
12     Given an integer, write a function to determine if it is a power of three.
13 
14     Follow up:
15     Could you do it without using any loop / recursion?
16 
17  ************************************************************************/
18 
19 #include "stdio.h"
20 
21 int isPowerOfThree(int n) {
22     double tmp = log10(n)/log10(3);
23     return tmp == (int)tmp ? 1 : 0;
24 }
25 
26 int main()
27 {
28     int n = 9;
29     int ret = isPowerOfThree(n);
30     printf("%d\n",ret);
31     
32     n = 10;
33     ret = isPowerOfThree(n);
34     printf("%d\n",ret);
35     
36     return 0;
37 }

 

LeetCode 326

标签:

原文地址:http://www.cnblogs.com/Juntaran/p/5479112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!