《算法竞赛入门经典》7744问题(1)

输出所有形如aabb的4位完全平方数(即前两位数字相等,后两位数字也相等)
#include<stdio.h>
#include<iostream>
#include<math.h>

using namespace std;

int main()
{
	for (int a = 1; a <= 9; a++)
	for (int b = 0; b <= 9; b++)
	{
		int n = a * 1100 + b * 11;
		int m = floor(sqrt(n) + 0.5);  //floor(x)返回不超过x的最大整数
		if (m*m == n)  //判断是否为完全平方数
		{
			cout << n << endl;
		}
	}
	return 0;
}
用“开平方”函 数,可以先求出其平方根,然后看它是否为整数,即用一个int型变量m存储sqrt(n)四舍五入后的整数,然后判断m2是否等于n。函数floor(x)返回不超过x的最大整数。

猜你喜欢

转载自blog.csdn.net/qq_38734403/article/details/80259926