PAT 实验5-6 使用函数判断完全平方数

本题要求实现一个判断整数是否为完全平方数的简单函数。
函数接口定义:
int IsSquare( int n );
其中n是用户传入的参数,在长整型范围内。如果n是完全平方数,则函数IsSquare必须返回1,否则返回0。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int IsSquare( int n );

int main()
{
int n;

scanf("%d", &n);
if ( IsSquare(n) ) printf("YES\n");
else printf("NO\n");

return 0;

}

int IsSquare( int n )
{
double a;
a = sqrt(n);
if ( floor(a + 0.5) == a) //判断a是否为正数
return 1;
else
return 0;
}
在这里插入图片描述

发布了67 篇原创文章 · 获赞 54 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/naturly/article/details/104044322
5-6