// windowsapplication.cpp : 定义控制台应用程序的入口点。
//题目: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?
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class solution{
public:
bool isPowerOfThree(int n){
double x = log10(n*1.0) / log10(3.0);
return (n > 0) && (ceil(x) == floor(x));
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"请输入n的值:";
cin>>n;
solution s;
if (s.isPowerOfThree(n)==true)
cout<< "it is a power of 3!"<<endl;
else
cout<<"it is not a power of 3!";
return 0;
}
//题目: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?
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class solution{
public:
bool isPowerOfThree(int n){
double x = log10(n*1.0) / log10(3.0);
return (n > 0) && (ceil(x) == floor(x));
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"请输入n的值:";
cin>>n;
solution s;
if (s.isPowerOfThree(n)==true)
cout<< "it is a power of 3!"<<endl;
else
cout<<"it is not a power of 3!";
return 0;
}