Blue Bridge Cup algorithm number explanations of previous questions walnuts

Title Description

Problem Description
Zhang is a software project manager, he led three development groups. Tight schedule today in overtime it. To boost morale, Zhang intends to send a bag of walnuts (rumored to be able to nourish the brain) to each group. His requirements are:

  1. Number of walnuts each group must be the same
  2. Each group must be equally Walnut (of course, not broken)
  3. Try to provide a minimum number satisfying the condition of 1 (saving revolution Well)

Input format
input contains three positive integers a, b, c, represents the number of each group is overtime, separated by a space (a, b, c <30 )
output format
output a positive integer representing the number of bag walnut.
Sample Input 1
2. 4. 5
Sample Output 1
20 is
a sample input 2
. 3. 1. 1
Sample Output 2
. 3

Problem solution :

First, to ensure the number of walnut is a multiple of the number of each group, because the group can be divided equally, and secondly to ensure that the number given to each group are the same, so in fact it is easy to think of is the least common multiple of the number three groups.

Code :

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;

int a,b,c;

int gcd(int x,int y)
{
    return x ? gcd(y%x,x) : y;
}

int lcm(int x,int y)
{
    return x / gcd(x,y) * y;
}

int main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */

    cin >> a >> b >> c;
    cout << lcm(lcm(a,b),c);

    return 0;
}
He published 197 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41708792/article/details/105331613