求整数唯一分解定理的算法!

shou3301 2009-04-23 08:09:43
Description
For a given integer, "Prime Factorization" is finding which prime numbers you need to multiply together to get the original number, also called prime decomposition.

Given a positive integer n>=2, the prime factorization is written

n = p1(a1)p2(a2)...pk(ak)

where the pis are the k prime factors, each of order ai.

Write a program to factor a given integer n.



Input
One integer, n. (2<=n<=231-1).



Output
Output the prime factorization of n as follows:

n=p1(a1)p2(a2)...pk(ak)

Where p1<p2<...<pk, and ai denotes the exponent of pi.

For example, for n = 1500 = 22*3*53, the output should be:

1500=2(2)3(1)5(3)



Sample Input #1
2


Sample Output #1
2=2(1)



Sample Input #2
15


Sample Output #2
15=3(1)5(1)



Sample Input #3
1001


Sample Output #3
1001=7(1)11(1)13(1)
...全文
217 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
继续睡 2009-04-24
  • 打赏
  • 举报
回复
我5楼的isprime函数本身就有问题,但是由于其根本不需要,所以结果正确。我晕......
脑子乱了...
继续睡 2009-04-24
  • 打赏
  • 举报
回复
没有isprime函数一样可以,好像效率还高些,怎么不能修改帖子......以下是代码
#include<iostream>
using namespace std;
main()
{
long i,n;
int count;
cin >> n;
for(i=2;i<n/2;i++)
{
if((n%i)== 0)
{
count = 0;
while((n%i) == 0)
{
n = n/i;
count++;
}
cout << i << "(" << count << ")";
}
}
if(n != 1)
cout << n << "(1)" << endl;
system("pause");
}

shou3301 2009-04-24
  • 打赏
  • 举报
回复
后来得到一位朋友相助,得到以下代码:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
unsigned int n;//要分解的n
int flag = 0;//标记
cin >> n;
int prime[32] = {0};//存储因子
int num[32] = {0};//对应因子个数
int startlocation = 0;//存储因子的起始位置
unsigned int start = 2;//起始试探因子
while (start*start <= n)
{
while (n % start == 0)
{
prime[startlocation] = start;
num[startlocation]++;
n /= start;
flag = 1;
}
if (flag)
{
startlocation++;
flag = 0;
}
start++;
}
int k = 0;
while (prime[k])
{
cout << prime[k] << "(" << num[k] << ")";
k++;
}
if (n != 1)
cout << n << "(" << 1 << ")" << endl;
return 0;
}

试验后正确!
shou3301 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zhaojunshi886 的回复:]
没有isprime函数一样可以,好像效率还高些,怎么不能修改帖子......以下是代码
#include <iostream>
using namespace std;
main()
{
      long i,n;
      int count;
      cin >> n;
      for(i=2;i <n/2;i++)
      {
              if((n%i)== 0)           
              {
                    count = 0;
                    while((n%i) == 0)
                    {
                        n = n/…
[/Quote]

和我自己的做法一模一样……有没有更好一点的做法啊?
继续睡 2009-04-23
  • 打赏
  • 举报
回复
我做这种算法题非常烂,时间肯定是超了......结果应该还正确吧..
#include<iostream>
using namespace std;
int isprime(int);
main()
{
long i,n;
int count;
cin >> n;
for(i=2;i<n/2;i++)
{
if(isprime(i) == 1)
{
if((n%i)== 0)
{
count = 0;
while((n%i) == 0)
{
n = n/i;
count++;
}
cout << i << "(" << count << ")";
}
}
}
if(n != 1)
cout << n << "(1)" << endl;
system("pause");
}
int isprime(int x)
{
int i,flag = 1;
for(i=2;(i<x/2)&&flag ==1;i++)
{
if(x&i==0)
flag = 0;
}
return flag;
}
honghu069 2009-04-23
  • 打赏
  • 举报
回复
构造2~2^16 之间的素数
用这些素数分解即可
shou3301 2009-04-23
  • 打赏
  • 举报
回复
忘了说一点~就是时间要控制在1000ms内哦~
baiwei156 2009-04-23
  • 打赏
  • 举报
回复
友情帮顶
liliangbao 2009-04-23
  • 打赏
  • 举报
回复
帮顶~

33,322

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧