leecode 解题总结:342. Power of Four

来源:互联网 发布:淘宝溢价是什么意思 编辑:程序博客网 时间:2024/06/05 00:10
#include <iostream>#include <stdio.h>#include <vector>#include <string>using namespace std;/*问题:Given an integer (signed 32 bits), write a function to check whether it is a power of 4.Example:Given num = 16, return true. Given num = 5, return false.Follow up: Could you solve it without loops/recursion?分析:一个数如果能被4整除,则该数Num必定存在log4(Num)的值为0注意都取10为底,取e为底,存在精度问题输入:165输出:truefalse关键:1 一个数如果能被4整除,则该数Num必定存在log4(Num)的值为0注意都取10为底,取e为底,存在精度问题*/class Solution {public:    bool isPowerOfFour(int num) {        double result = log10(num) / log10(4);return (result - (int)result) == 0 ? true : false;    }};void process(){ int num; Solution solution; while(cin >> num ) { bool result = solution.isPowerOfFour(num); if(result) { cout << "true" << endl; } else { cout << "false" << endl; } }}int main(int argc , char* argv[]){process();getchar();return 0;}

0 0
原创粉丝点击