cjweffort

博客园 首页 联系 订阅 管理

http://ac.jobdu.com/problem.php?cid=1040&pid=55

题目描述:

输入n个整数,依次输出每个数的约数的个数

输入:

输入的第一行为N,即数组的个数(N<=1000)
接下来的1行包括N个整数,其中每个数的范围为(1<=Num<=1000000000)
当N=0时输入结束。

输出:

可能有多组输入数据,对于每组输入数据,
输出N行,其中每一行对应上面的一个数的约数的个数。

样例输入:
5
1 3 4 6 12
样例输出:
1
2
3
4
6
// 题目56:约数的个数.cpp: 主项目文件。

#include "stdafx.h"
#include <cstdio>
#include <cstring>
#include <math.h>

const int N=31650;
bool prime[N];

void selectPrime()
{
	memset(prime,0,sizeof(prime));
	prime[0]=prime[1]=true;
	for(int i=2;i<N;i++)
	{
		if(!prime[i])
		{
			for(int j=i+i;j<N;j+=i)
				prime[j]=true;
		}
	}
}

int baseCount(int num)
{
	int res=1;
	int tmp=(int)sqrt(1.0*num);
	for(int i=2;i<=tmp;i++)
	{
		if(num==1)
			break;
		if(!prime[i])
		{
			int tmpCnt=0;
			while(num%i==0)
			{
				tmpCnt++;
				num/=i;
			}
			res*=(tmpCnt+1);
		}
	}
	if(num!=1)
		res*=2;
	return res;
}

int main()
{
    int n;
	selectPrime();
	while(scanf("%d",&n)==1&&n)
	{
		while(n--)
		{
			int num;
			scanf("%d",&num);
			int res=baseCount(num);
			printf("%d\n",res);
		}
	}
    return 0;
}


posted on 2013-03-06 11:35  cjweffort  阅读(232)  评论(0编辑  收藏  举报