您的位置:首页 > 其它

HDU 5108 Alexandra and Prime Numbers

2014-12-03 22:14 423 查看

Alexandra and Prime Numbers

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1019 Accepted Submission(s): 358


Problem Description
Alexandra has a little brother. He is new to programming. One day he is solving the following problem: Given an positive integer N, judge whether N is prime.

The problem above is quite easy, so Alexandra gave him a new task: Given a positive integer N, find the minimal positive integer M, such that N/M is prime. If such M doesn't exist, output 0.

Help him!
Input
There are multiple test cases (no more than 1,000). Each case contains only one positive integer N.

N≤1,000,000,000

.

Number of cases with N>1,000,000


is no more than 100.
Output
For each case, output the requested M, or output 0 if no solution exists.
Sample Input
3
4
5
6


Sample Output
1
2
1
2[code]#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int main()
{
	int n,i,ans,cnt;
	
	while(scanf("%d",&n)!=EOF)
	{
		if(n==1)
		{
			printf("0\n");
			continue;
		}
		ans=n;
		for(i=2;i*i<=n;i++)
		{
			if(n%i==0)//每次去除质因数 
			{
				while(n%i==0)
					n/=i;	
				cnt=i;//保存最大的质因数 
			}	
		}
		if(n>1)//若不是1,则剩余n为质数 ,否则被完全分解,除 
			cnt=n;
		printf("%d\n",ans/cnt);
	} 	
	return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: