HDU-Eddy's research I-拆分素数

79 篇文章 0 订阅
问题及代码:

Eddy's research I

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

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

Author

eddy

/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.    
*文件名称:HDU.cpp    
*作    者:单昕昕    
*完成日期:2015年2月9日    
*版 本 号:v1.0        
*/ 

//<span style="font-family: Verdana, Arial, Helvetica, sans-serif;">AC代码1</span>
#include <iostream>
#include <cmath>
using namespace std;
int prime(int a)
{
    int i;
    for(i=2; i<sqrt(a); i++)
    {
        if(a%i == 0)
            return 0;
    }
    return 1;
}
int main()
{
    int n, i;
    while(cin>>n)
    {
        while(n!=1)
        {
            for(i = 2; i <= n; i++)
            {
                if(n%i == 0)
                {
                    if(prime(i))
                    {
                        n /= i;
                        cout<<i;
                        if(n!= 1)
                            cout<<"*";
                        break;
                    }
                }
            }
        }
        cout<<endl;
    }
    return 0;
}
//AC代码2,素数先存后用。
#include <iostream>
using namespace std;
bool prime(int n)   //判断一个数是不是素数
{
    for(int i=2; i<n; i++)
        if(n%i==0)
            return 0;
    return 1;
}
bool a[65536];  //存储素数
int b[65536];
int main()
{
    int x;
    while(cin>>x)
    {
        int n= 0;
        int num = x;
        int i=2;
        while(num!=1)
        {
            if(num%i==0)    //x被i整除
            {
                if(prime(i))
                {
                    a[i] = true;
                    b[++n] = i;
                    num/=i;
                    i=1;
                }
            }
            i++;
        }
        for(i=1; i<n; i++)
            cout<<b[i]<<'*';
        cout<<b[i]<<endl;
    }
    return 0;
}//AC代码3
#include <iostream>
using namespace std;
bool prime(int n)   //判断一个数是不是素数
{
    for(int i=2; i<n; i++)
        if(n%i==0)
            return 0;
    return 1;
}
bool a[65536];  //存储素数
int main()
{
    int x;
    while(cin>>x)
    {
        int n,num,i;
        n= 0;
        num = x;
        i=2;
        while(num!=1)
        {
            if(num%i==0)    //x被i整除
            {
                if(prime(i))
                {
                    a[i] = true;
                    cout<<i;
                    num/=i;
                    if(num!= 1)
                        cout<<"*";
                    i=1;//保证每次都取最小质因数
                }
            }
            i++;
        }
        cout<<endl;
    }
    return 0;
}



运行结果:




知识点总结:

一开始想到用穷举法,先写一个判断素数的函数,由于 1 < x<= 65535,所以穷举出x的质因数,按题目的形式输出就行了。
可是……超时了……


我看了半天没看出来哪还能优化,数了数我用了五个循环,是有点多,但是应该没啥影响啊。
再一看,我在第一个循环里就先判断是否为素数,是就输出原数,否就进入下面的拆分循环。
其实这一步是多余的,时间就浪费在这第一步判断中了,虽然我刚写这题的时候觉得这个判断很符合题意。然后我删除这第一个判断后,就AC了。



学习心得:

这题要用数组保存需要用的变量然后备用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值