题解 | #约数的个数#

约数的个数

https://www.nowcoder.com/practice/04c8a5ea209d41798d23b59f053fa4d6

#include <bits/stdc++.h>
using namespace std;

int yueshu(int x){
    if(x == 1) return 1;
    else if(x == 0) return 0;
    else {
        int count = 0;
        int newx = sqrt(x);
        for(int i = 1;i <= newx;i++){
            if(x % i == 0) count = count + 2;
        }

        if(newx *newx == x) count --;
        return count;
    }
}


int main(){
    int n;
    long long number;
    while(cin >> n){
        for(int i = 0 ; i < n; i ++){
            cin >> number;
            cout << yueshu(number) << endl;
        }
    }
}

判断约数的个数,可以对数字的根号sqrt为终值,如果存在则+2,如果sqrt*sqrt等于数字本身,则结果最后再减去1

#算法#
全部评论

相关推荐

02-25 19:38
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务