Example: Output the four-digit complete square number of aabb (the first two digits are the same, the last two digits are the same)
Must pay attention to the way of thinking:
- Square root (but will produce errors)
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
int a;
int b;
for (int i = 1; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int n = i * 1100 + j * 11;
int m = floor(sqrt(n) + .5);//要避免误差的影响(比如 ,经过大量运算之后,由于误差,1变成了0.99999);---->此处在消除误差;
if (m * m == n)printf("%d", n);
}
}
}
- Avoid square root:
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
for (int i = 1;; i++)
//此处可以从32开始数,因为之前的数都不足四位;
{
int n = i * i;
if (n < 1000)continue;
if (n > 9999)break;
int a = n / 100;
int b = n % 100;
if (a % 10 == a / 10 && b % 10 == b / 10)printf("%d", n);
}
}