HDU1164质数

来源:互联网 发布:彩票开奖数据采集源码 编辑:程序博客网 时间:2024/06/05 12:39

Eddy’s research I

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

Problem Description
Eddy’s interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can’t write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.

Output
You have to print a line in the output for each entry with the answer to the previous question.

Sample Input
11
9412

Sample Output
11
2*2*13*181


思路

题意就是输入一个数,然后这个数由几个质数相乘得到,如果这个是质数,直接输出


代码

#include <iostream>#include <stdio.h>using namespace std;bool is_prime(int n){//判断质数    if (n < 2){        return false;    }    int i;    for (i = 2; i*i <= n; i++){        if (n%i == 0){            return false;        }    }    return true;}int main(){    int n;    while(cin>>n)    {        int i=2;        int m=1;        if(is_prime(n))            cout<<n<<endl;            else            {                int i=2;                while(i<=n)                {                    if(n%i==0)                    {                        if(n==i)                        {                            cout<<n<<endl;                            break;                        }                        else                            cout<<i<<"*";                        n/=i;                    }                    else                    {                        i++;                        continue;                    }                }            }    }    return 0;}
原创粉丝点击