LINTCODE—— 3个不同的因子

来源:互联网 发布:ubuntu设置不休眠 编辑:程序博客网 时间:2024/06/07 01:35

LINTCODE—— 3个不同的因子

思路:加入n有三个不同的因子,那么只能是1,n,还有就是n的开方了,并且sqrt(n)为质数,然后就没有然后了。。。。

class Solution {public:    /*     * @param : the given number     * @return:  return true if it has exactly three distinct factors, otherwise false     */    bool isThreeDisctFactors(long long n) {        // write your code here        if(n<=3)            return false;        long long cnt = sqrt(n);        if(cnt*cnt!=n)            return false;        //判断cnt是否为质数        n = cnt;        cnt = sqrt(cnt);        //bool is_flag = true;        for(int i = 2; i<=cnt; i++)        {            if(n%i==0)                return false;        }        return true;    }};
原创粉丝点击