分享

c++正整数因式分解

 163九九 2016-04-10

最新c++正整数因式分解

以下是三零网为大家整理的最新c++正整数因式分解的文章,希望大家能够喜欢!

#include <iostream>
#include <map>
#include <set>

using namespace std;

void fact(int a, map<int, int>& result);

int main()
{
int a = 1025;
map<int, int> m;
fact(a, m);

cout << a << " = ";
for(map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << "^" << it->second << " ";
}
cout << endl;

cin.get();
}

void fact(int a, map<int, int>& result)
{
multiset<int> exp;
while(a > 1)
{
for(int i = 2; i <= a; i++)
{
if(0 == a % i)
{
a = a / i;
exp.insert(i);
break;
}
}
}

for(multiset<int>::iterator it = exp.begin(); it != exp.end(); )
{
result.insert(make_pair(*it, exp.count(*it)));
it = exp.upper_bound(*it);
}
}
 


延伸阅读:
·C语言经典算法 - 因式分解
·c语言 输出所有小于n(n>=2正整数)的素数
·C语言输入两个正整数m和n,求其最大公约数和最小公倍数
·C语言将一个正整数分解质因数
·c++ 输入两个正整数m和n,求其最大公约数和最小公倍数
·C语言正整数转换成罗马数字

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多