题目描述:
输出所有形如aabb的四位完全平方数(即前两位数字相等,后两位数字也相等)
分析:
floor(x)函数:返回不超过x的最大整数。
判断条件:一个数开方后取整,如果平方还是它本身的话,证明是完全平方数。
注意:
大整数开方的时候,由于误差的影响,整数1变成了0.999999999,这样算得的结果就会是0而不是1,为了减小误差影响,一般采用四舍五入的方法,即:floor(sqrt(x)+0.5)
(小数部分为0.5的数也会受到浮点误差的影响,等后面学习了之后再来更新)。
代码:
#include<iostream>
#include<string>
#include<cstring>
#include<Windows.h>
#include<cmath>
using namespace std;
int main()
{
int a, b, n;
for (int i = 1;i <= 9;i++)
{
for (int j = 0;j <= 9;j++)
{
n = 1100 * i + 11 * j;
a = floor(sqrt(n) + 0.5);
if (a*a == n)
cout << n << ' ';
}
}
system("pause");
return 0;
}