HDU-#5108 Alexandra and Prime Numbers

        题目大意:给一个数N,求一个最小的M,使得N/M是素数。

        解题思路:好久没做比赛了,手已经生的不行了,自己被Hacker了不说,Hacker别人居然也被抢了,实在不应该....好久不做题就各种手残呀....本题就直接反过来求即可,要求最小的M,则要求N最大的素因子。对于利用数的素因子拆分原理求解即可,特别注意需要题目为0的情况是N为1时。

        题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=5108

       code:

#include 
#include 
#include 
using namespace std;

int n,ans;

int main(){
        while(~scanf("%d",&n)){
                if(n==1) printf("0\n");
                else{
                        int cnt=n;
                        for(int i=2;i*i<=cnt;++i)
                                while(cnt%i==0){
                                        cnt/=i;
                                        ans=i;
                                }
                        if(cnt>1) ans=cnt;
                        printf("%d\n",n/ans);
                }
        }
        return 0;
}

你可能感兴趣的:(HDU-#5108 Alexandra and Prime Numbers)