完全平方:能被开方
方法一:
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main(){
int a,b;
for(a=1;a<10;a++){
for(b=0;b<10;b++){
int n=1100*a+11*b;
int m=floor(sqrt(n)+0.5);
if(m*m==n) cout<<n;
}
}
system("pause");
return 0;
}
floor:返回一个参数的最大整数
+0.5:为了保证四舍五入
先加根号,再开根号,看是否一样
方法二:
int main(){
for(int x=1; ;x++){
int n=x*x;
if(n<1000) continue;
if(n>9999) break;
int hi=n/100;
int lo=n%100;
if(hi/10==hi%10&&lo/10==lo%10) cout<<n;
}
system("pause");
return 0;
}
定义一个变量x,x不断+1
n为x的平方,n是一个四位数
hi为aa,lo为bb,如果n的第一位和第二位相等,第三位和第四位相等,满足aabb形式。与方法一不同的是那个是开根号,这个是判断位数,省了开方的过程。

1521

被折叠的 条评论
为什么被折叠?



