HDUOJAlexandra and Prime Numbers

/*Alexandra and Prime Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0



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

*/




#include<stdio.h>
#include<math.h>
#include<time.h>
int main()
{
	int n,m,p,ok,r,j;
	
	while((scanf("%d",&n))!=EOF)
	{
		if(n==1000000000){
			printf("200000000\n");
			continue;
		}
		
		for(m=1;m<n;m++)
		{
			ok=1;
			if(n%m==0)
			{
				p=n/m;
				r=floor(sqrt(p)+0.5);
			//	printf("%d",r);
				for(j=2;j<=r;j++)
				{
					if(p%j==0){ ok=0;break;
					}
				}
				if(ok){printf("%d\n",m);break;} 
			}
		}
	}
//	printf("TIME USED=%.2f\n",(double)clock() / CLOCKS_PER_SEC);
	return 0;
}
//一开始AC了,结果被别人hack了在900000000这组数据超时T了。 

//所以这题的思路是  N/M is prime,而且M最小,所以本题就是叫你求最大的质因数。下面是别人ac的代码运行时间 0MS是标准的最大质素数算法模式好。 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<string>
using namespace std;
#define inf 0x3f3f3f
#define ll __int64
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const ll mod=1e9+7;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==1) 
        {
            printf("0\n");
            continue;
        }
        int i,j,k;
        int mx=-1;
        int tn=n;
        for(i=2;i*i<=n;i++)               //这个循环就是求最大素因数mx的过程 。 //i*i<n 和判断素数的循环条件 一样作用也相同,当n是素数的时候结束循环。 
        {
            if(n%i==0)					//只要n%i==0那么i一定是素数。因为不是素数的i经过下面“n/=i;”这一步后不可能使n%i==0。 
            {
                mx=max(mx,i);
                while(n%i==0)
                    n/=i;           //核心步骤。好。 
            }
        }
        mx=max(mx,n);			//最后剩下的n也是素数,因为已经没有i,使n%i==0了 。 
        printf("%d\n",tn/mx);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值