C++编程 Eddy's research I

来源:互联网 发布:金螳螂网络采购平台 编辑:程序博客网 时间:2024/05/16 15:00

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

119412

Sample Output

112*2*13*181

本题目就是先算出来一共多少质数(Prime)然后再用一个数组记录下身为因子的质数的个数。


#include <iostream>#include <cmath>#include <cstring>#define MAX 65535bool v[MAX];void prime(){    memset(v, true, sizeof(v));    v[0] = v[1] = false;    for(int i = 2; i < sqrt(MAX); i++)    {        if(v[i])        {            for(int j = 2*i; j < MAX; j+=i)            v[j] = false;        }    }}using namespace std;int main(){    long n;    int num[20];    prime();    int temp;    while(cin>>n)    {        temp = 0;         for(int i = 2; i < MAX; i++)        {            if(v[i])            {                if(n % i == 0)                {                    while(n % i == 0)                    {                        n /= i;                        num[temp++] = i;                    }                }            }        }        for(int i = 0; i < temp; i++)        {            cout<<num[i];            if(i!=temp-1)            {                cout<<"*";            }            else            {                cout<<endl;            }        }    }    return 0;}


原创粉丝点击