清华-约数的个数

题目:

多组输入,输入一个数n,代表数的个数,然后输入n个数,输出每个数的约数个数。

输入样例:

5
1 3 4 6 12

  

输出样例:

1
2
3
4
6

  

code:

#include<bits/stdc++.h>

using namespace std;

#define maxn 10010

int arr[maxn];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++) cin>>arr[i];
        for(int i=0;i<n;i++){
            int x=arr[i];
            int ans=0;
            int sx=sqrt(x+0.5);
            for(int i=1;i<=sx;i++){
                if(x%i==0){
                    ans+=2;
                    if(i*i==x) ans-=1;
                }
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zscoder/p/10624073.html