我的思路把他们都转化为字符串比较末尾长度,这个长度为想要判断的整数长度。
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int n;
while(cin>>n){
int cnt = 0;
bool guard =false;
for(int x =0;x<=n;x++){
guard =false;
string strx;
strx=to_string(x);
int y=pow(x,2);
string stry;
stry=to_string(y);
for(int i = stry.size()-strx.size(),j=0;i<stry.size();i++){
if(stry[i]==strx[j++])guard=true;
else {guard=false;
break;}
}
if(guard)cnt++;
}
cout<<cnt<<endl;
}
}