1. 程式人生 > >求兩個數的最小公倍數

求兩個數的最小公倍數

個數 actor arr == int ++ str div osi

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

/*
    求最小公倍數
*/
vector<int> Decomposition(int digit)
{
    int actor = digit;
    vector<int> arr;
    for (int i = 2;i < digit;i ++)
    {
        while (actor % i == 0)
        {
            actor 
= actor / i; arr.push_back(i); } } arr.push_back(1); return arr; } vector<int> Get(vector<int> first,vector<int> second) { vector<int> result; sort(first.begin(),first.end()); sort(second.begin(),second.end()); set_union(first.begin(),first.end(),second.begin(),second.end(),back_inserter(result));
return result; } void Display(vector<int> such) { for (int i = 0;i < such.size();i ++) { cout<<such[i]<<" "; } cout<<endl; } int Common(int digit_one,int digit_two) { int result = 1; vector<int> first= Decomposition(digit_one); vector
<int> second= Decomposition(digit_two); vector<int> beauty = Get(first,second); /* Display(first); Display(second); Display(beauty); */ for (int i = 0;i < beauty.size(); i ++) { result = result * beauty[i]; } cout<<result<<endl; return result; } int main() { Common(45,30); return 0; }

求兩個數的最小公倍數