当前位置:网站首页>【码蹄集新手村600题】判断一个数字是否为完全平方数
【码蹄集新手村600题】判断一个数字是否为完全平方数
2022-08-01 13:05:00 【Sandm *】
题目链接:
解题思路:
一个数开方之后,只有整数部分而没有小数部分,则该数字为完全平方数
根据完全平方数的概念,
其中通过 pow() 函数去进行这个数的开方,
但是因为 pow() 函数的返回类型是 double,
即使这个数开方之后,
即使不是完全平方数,但也会得到一个经过开方得到的值,
所以我们怎么能具体判断它是不是完全平方数呢?
因为完全平方数没有小数部分,
所以它经过开方,然后再平方,
其值仍然为之前的初始值,
而如果这个数不是完全平方数,
经过开方之后,
我们可以通过对 pow() 函数的返回值进行 int 类型的强转,
从而得到其整数部分,
此时整数部分经过平方后就不与它未开方时的数相等,
由此可以作为我们的判断依据
参考代码:
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int data = 0;
cin >> data;
int change = pow(data,1/2.0);
if( change * change == data)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
边栏推荐
猜你喜欢
【StoneDB Class】Introduction Lesson 2: Analysis of the Overall Architecture of StoneDB
Alibaba Cloud Official Redis Development Specification
uniapp读取和写入文件
消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?
数据挖掘-04
Data Mining-04
Windows 安装PostgreSQL
leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
程序员的浪漫七夕
50W+小程序开发者背后的数据库降本增效实践
随机推荐
Do wildcard SSL certificates not support multiple domains?
How do we do full-link grayscale on the database?
AI目标分割能力,无需绿幕即可实现快速视频抠图
【2022蓝帽杯】file_session && 浅入opcode
透过开发抽奖小程序,体会创新与迭代
AD单片机九齐单片机NY8B062D SOP16九齐
MySQL调优
那些利用假期学习的职场人,后来都怎么样了?
This article will take you to thoroughly clarify the working mechanism of certificates in Isito
SQL function SQRT
NFV迈向云原生时代:Network Service Mesh项目介绍
JMP Pro 16.0软件安装包下载及安装教程
MVVM响应式
leetcode: 1201. Ugly Number III [Dichotomy + Mathematics + Inclusion and Exclusion Principle]
formatdatetime function mysql (date sub function)
tensorflow2.0 handwritten digit recognition (tensorflow handwriting recognition)
Six Stones Programming: Problems must be faced, methods must be skillful, and functions that cannot be done well must be solved
PAT1165 Block Reversing(25)
Apex installation error
Detailed explanation of table join