【MAC 上学习 C++】Day 39-5. 实验5-6 使用函数判断完全平方数 (10 分)

实验5-6 使用函数判断完全平方数 (10 分)

1. 题目摘自

https://pintia.cn/problem-sets/13/problems/464

2. 题目内容

本题要求实现一个判断整数是否为完全平方数的简单函数。

函数接口定义:

int IsSquare( int n );
其中n是用户传入的参数,在长整型范围内。如果n是完全平方数,则函数IsSquare必须返回1,否则返回0。

输入样例1:

10

输出样例1:

NO

输入样例2:

100

输出样例2:

YES

3. 源码参考
#include 
#include 

using namespace std;

int IsSquare( int n );

int main()
{
    int n;

    cin >> n;
    if(IsSquare(n))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}

int IsSquare( int n )
{
    double s;

    s = sqrt(n);
    if(s - (int)s == 0)
    {
        return 1;
    } 

    return 0;
}

你可能感兴趣的:(【MAC 上学习 C++】Day 39-5. 实验5-6 使用函数判断完全平方数 (10 分))