牛客网 对称平方数解题

题目描述

打印所有不超过n(n<256)的,其平方具有对称性质的数。如11*11=121。
 
解题:
#include <iostream>
#include <cmath>
using namespace std;
class project{
public:
    void duicheng(){
        string s;
        string::iterator p, q;
        for(int i=1; i<256*256; i++){
            s=to_string(i);
            p=s.begin(),q=s.end()-1;
            while(p!=q && *p==*q){
                p++;
                q--;
            }
            if(*p!=*q || ((int)(((int)sqrt(i)) * ((int)sqrt(i))) != i)){   
                continue;
            }
            else{
                cout << sqrt(i) << endl;
                continue;
            }
        }
        
    }
};

int main(int argc, char** argv) 
{
    project P;
    P.duicheng();
    return 0;
}

((int)(((int)sqrt(i)) * ((int)sqrt(i))) != i)可用于判断开方结果是否为整数。

算法效率:

猜你喜欢

转载自www.cnblogs.com/HonkerYblogs/p/10541024.html