CCNU第五周赛前练习:将一个数分解成质因子相乘的形式

K - Number theory-2
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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 额题目已经说得很明白了,将一个数分解成质因子相乘的形式,唔这个数最大只有65536 粗估了一下也就是2*3*5*7*11*13*17的范围内反正数组开大点也应该没事- -,所以我 们需要做的又只是三件事:1.判断素数 2.筛选并存下这个大范围内的素数 3.对这个数进 行质因子分解,当然这题输出格式上得注意一下- - 素数直接输出很好写,主要是连乘的 形式我一开始没注意导致每个可以质因子分解的数最后多了个*出来ORZ,不过后面改过来 就AC了下面贴上代码,代码里面也有一点注释可以看看。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int prime[100005];

bool check(int n)
{
    int i;
    if(n==2) return true;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
            return  false ;

    }
    return true;
}
int main()
{
    int n;
    int i,j=0;
    int a[1005];

    for(i=2;i<=65536;i++)//暴筛这个范围内的所有素数存进数组
    {
        if(check(i)==true)
        {
            prime[j++]=i;
        }
    }
    while(cin>>n)
    {   int k=0,flag=0;
        if(check(n)==true)
            cout<<n<<endl;
        else
        {

            while(n>1)
            {
                if(n%prime[flag]==0)//如果能被这个素数整除则数分解
                {
                    a[k++]=prime[flag];
                    n=n/prime[flag];
                }
                else
                    flag++;//否则找筛选出来的素数里的下一个素数进行判断
            }

            for(int t=0;t<k;t++)
            {   if(t==k-1)
                printf("%d\n",a[t]);//如果到了最后一个直接输出来就好了不用*
                else
                printf("%d*",a[t]);//否则每次带个*号
            }

        }
    }
    return 0;



}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值