CCF NOI1024. 因子個數
阿新 • • 發佈:2018-12-09
1024. 因子個數
題目描述
對於任意給定的一個正整數,計算其因數個數。
輸入樣例:
6
輸出樣例:
4
說明:
1、2、3、6都是6的因數。因此,輸出4。
輸入
輸入正整數N。
輸出
輸出N的因子個數。
樣例輸入
6
樣例輸出
4
資料範圍限制
1<=N<2^31
C++程式碼
#include <iostream>
#include <cmath>
#include <cassert>
using namespace std;
int main ()
{
int N;
cin >> N;
assert(N >= 1);
int numOfDivisors;
if (1 == N)
{
numOfDivisors = 1; // 1 has only one divisor
}
else
{
numOfDivisors = 2; // 1 and itself are its divisor
for(int i=2; i<=(int)sqrt((double)N); i++)
{
if (N%i == 0)
{
if (i*i == N)
{
numOfDivisors++; // i = N/i only divisor
}
else
{
numOfDivisors +=2; // i and N/i are its divisors
}
}
}
}
cout << numOfDivisors << endl;
return 0;
}