完美的方形和完善立方 [英] Perfect square and perfect cube

查看:187
本文介绍了完美的方形和完善立方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在C ++中的任何predefined函数来检查的数量是否是方的任何数量和相同的立方体。

Is there any predefined function in c++ to check whether the number is square of any number and same for the cube..

推荐答案

没有,但它很容易写一个:

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;
}

这篇关于完美的方形和完善立方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆