C++ 完美的正方形和完美的立方体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1549941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Perfect square and perfect cube
提问by d3vdpro
Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..
c++中是否有任何预定义的函数来检查数字是否为任何数字的平方并且对于立方体是否相同..
回答by Chris Jester-Young
No, but it's easy to write one:
不,但写一个很容易:
bool is_perfect_square(int n) {
if (n < 0)
return false;
int root(round(sqrt(n)));
return n == root * root;
}
bool is_perfect_cube(int n) {
int root(round(cbrt(n)));
return n == root * root * root;
}
回答by Jesse Beder
sqrt(x)
, or in general, pow(x, 1./2)
or pow(x, 1./3)
sqrt(x)
,或一般而言,pow(x, 1./2)
或pow(x, 1./3)
For example:
例如:
int n = 9;
int a = (int) sqrt((double) n);
if(a * a == n || (a+1) * (a+1) == n) // in case of an off-by-one float error
cout << "It's a square!\n";
Edit: or in general:
编辑:或一般:
bool is_nth_power(int a, int n) {
if(n <= 0)
return false;
if(a < 0 && n % 2 == 0)
return false;
a = abs(a);
int b = pow(a, 1. / n);
return pow((double) b, n) == a || pow((double) (b+1), n) == a;
}
回答by Douglas B. Staple
No, there are no standard c or c++ functions to check whether an integer is a perfect square or a perfect cube.
不,没有标准的 c 或 c++ 函数来检查整数是完美的正方形还是完美的立方体。
If you want it to be fast and avoid using the float/double routines mentioned in most of the answers, then code a binary search using only integers. If you can find an n with n^2 < m < (n+1)^2, then m is not a perfect square. If m is a perfect square, then you'll find an n with n^2=m. The problem is discussed here
如果您希望它快速并避免使用大多数答案中提到的 float/double 例程,则仅使用整数编码二进制搜索。如果你能找到 n^2 < m < (n+1)^2 的 n,那么 m 不是一个完美的平方。如果 m 是一个完美的正方形,那么你会找到一个 n,其中 n^2=m。问题在这里讨论
回答by nikoo28
Try this:
尝试这个:
#include<math.h>
int isperfect(long n)
{
double xp=sqrt((double)n);
if(n==(xp*xp))
return 1;
else
return 0;
}
回答by Deepeshkumar
For identifying squares i tried this algorithm in java. With little syntax difference you can do it in c++ too. The logic is, the difference between every two consecutive perfect squares goes on increasing by 2. Diff(1,4)=3 , Diff(4,9)=5 , Diff(9,16)= 7 , Diff(16,25)= 9..... goes on. We can use this phenomenon to identify the perfect squares. Java code is,
为了识别正方形,我在 java 中尝试了这个算法。几乎没有语法差异,您也可以在 C++ 中做到这一点。逻辑是,每两个连续完美平方之间的差异继续增加 2。 Diff(1,4)=3 , Diff(4,9)=5 , Diff(9,16)= 7 , Diff(16,25 )= 9 ..... 继续。我们可以使用这种现象来识别完美的正方形。Java代码是,
boolean isSquare(int num){
int initdiff = 3;
int squarenum = 1;
boolean flag = false;
boolean square = false;
while(flag != true){
if(squarenum == num){
flag = true;
square = true;
}else{
square = false;
}
if(squarenum > num){
flag = true;
}
squarenum = squarenum + initdiff;
initdiff = initdiff + 2;
}
return square;
}
To make the identification of squares faster we can use another phenomenon, the recursive sum of digits of perfect squares is always 1,4,7 or 9. So a much faster code can be...
为了更快地识别平方,我们可以使用另一种现象,完美平方的递归数字总和始终为 1、4、7 或 9。因此,一个更快的代码可以......
int recursiveSum(int num){
int sum = 0;
while(num != 0){
sum = sum + num%10;
num = num/10;
}
if(sum/10 != 0){
return recursiveSum(sum);
}
else{
return sum;
}
}
boolean isSquare(int num){
int initdiff = 3;
int squarenum = 1;
boolean flag = false;
boolean square = false;
while(flag != true){
if(squarenum == num){
flag = true;
square = true;
}else{
square = false;
}
if(squarenum > num){
flag = true;
}
squarenum = squarenum + initdiff;
initdiff = initdiff + 2;
}
return square;
}
boolean isCompleteSquare(int a){
// System.out.println(recursiveSum(a));
if(recursiveSum(a)==1 || recursiveSum(a)==4 || recursiveSum(a)==7 || recursiveSum(a)==9){
if(isSquare(a)){
return true;
}else{
return false;
}
}else{
return false;
}
}
回答by Prashant Shubham
For perfect square you can also do:
对于完美的正方形,您还可以执行以下操作:
if(sqrt(n)==floor(sqrt(n)))
return true;
else
return false;
For perfect cube you can:
对于完美的立方体,您可以:
if(cbrt(n)==floor(cbrt(n)))
return true;
else
return false;
Hope this helps.
希望这可以帮助。
回答by Gautham
回答by Saurabh Kumar
The most efficient answer could be this
最有效的答案可能是这个
int x=sqrt(num)
if(sqrt(num)>x){
Then its not a square root}
else{it is a perfect square}
This method works because of the fact that x is an int and it will drop down the decimal part to store only the integer part. If a number is perfect square of an integer, its square root will be an integer and hence x and sqrt(x) will be equal.
这种方法之所以有效,是因为 x 是一个整数,它会降低小数部分以仅存储整数部分。如果一个数是一个整数的完全平方,它的平方根将是一个整数,因此 x 和 sqrt(x) 将相等。