核桃的数量

问题描述
小张是软件项目经理,他带领3个开发组。工期紧,今天都在加班呢。为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑)。他的要求是:

  1. 各组的核桃数量必须相同
  2. 各组内必须能平分核桃(当然是不能打碎的)
  3. 尽量提供满足1,2条件的最小数量(节约闹革命嘛)

输入格式
输入包含三个正整数a, b, c,表示每个组正在加班的人数,用空格分开(a,b,c<30)
输出格式
输出一个正整数,表示每袋核桃的数量。
样例输入1
2 4 5
样例输出1
20
样例输入2
3 1 1
样例输出2
3

再将unordered_map改成map,把auto遍历改成迭代器遍历后,通过。

#include<iostream>
#include<map>

using namespace std;

int a, b, c;
map<int, int> primes;

void get(int x){
    for(int i = 2; i <= x / i; i ++){
        if(x % i == 0){
            int cnt = 0;
            while(x % i == 0) x /= i, cnt ++;
            primes[i] = max(primes[i], cnt);
        }
    }
    primes[x] = max(primes[x], 1);
}

int main(){
    cin >> a >> b >> c;
    
    get(a), get(b), get(c);
    
    int res = 1;
    
    map<int, int> :: iterator it = primes.begin();
    
    while(it != primes.end()){
        int cnt = (*it).second;
        while(cnt --) res *= (*it).first;
        it ++;
    }
    
    cout << res << endl;
    
    return 0;
}
原文地址:https://www.cnblogs.com/tomori/p/13763623.html