c++编写一个函数,接受一个整数 n,判断它是否为完全平方数(一个数是某个整数的平方)。
作者:野牛程序员:2023-08-11 20:23:30 C++阅读 2682
c++编写一个函数,接受一个整数 n,判断它是否为完全平方数(一个数是某个整数的平方)。
#include <iostream>
bool isPerfectSquare(int n) {
if (n < 0) {
return false;
}
int root = static_cast<int>(sqrt(n));
return root * root == n;
}
int main() {
int num = 16;
bool perfectSquare = isPerfectSquare(num);
if (perfectSquare) {
std::cout << num << " is a perfect square." << std::endl;
} else {
std::cout << num << " is not a perfect square." << std::endl;
}
return 0;
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

