现在的位置: 首页 > 综合 > 正文

HDU 5108 Alexandra and Prime Numbers

2018年01月19日 ⁄ 综合 ⁄ 共 1013字 ⁄ 字号 评论关闭

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.
N1,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
#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;
}	

抱歉!评论已关闭.