欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SDNUOJ——1354.Cut the Cable

程序员文章站 2022-07-15 12:51:47
...

题目链接:1354.Cut the Cable

Description
The company have n(n<=100) long cables(the length is not necessarily the same),we assume that the length of every long cable is an integer and not exceeded 10000 meters. Now we cut them into short cables with the same length. We are required not to waste every long cable and the length of the short cable is the longer the better. Please calculate the longest length of short cable and how many short cables after cutting.
Input
The input contains two lines.
The first line is n, represents the number of long cables.
The second line has n positive integers separated by spaces, represent the length of long cable.

Output
The first line represent the longest length of short cable.
The second line represent the number of short cables.

Sample Input

4
18 12 24 30

Sample Output

6

分析:gcd暴力

//#include<bits/stdc++.h>
#include  <stdio.h>
#include <iostream>
#include<algorithm>
#include      <map>
#include      <set>
#include   <vector>
#include    <queue>
#include    <stack>
#include <stdlib.h>
#include  <cstring>
#include <string.h>
#include   <string>
#include   <math.h>
using namespace std;
typedef long long ll;
#define MAXN 10005
#define INF 0x3f3f3f3f//将近ll类型最大数的一半,而且乘2不会爆ll

int a[105];
int gcd(int a, int b)
{
    return !b? a: gcd(b, a%b);
}

int main()
{
    int n, minn, sum=0;
    cin >> n;
    for(int i=0; i<n; ++i)
        scanf("%d", &a[i]);
    minn = a[0];
    for(int i=1; i<n; ++i)
        minn = gcd(minn, a[i]);
    for(int i=0; i<n; ++i)
        sum+=a[i]/minn;
    cout << minn << '\n' << sum << '\n';
    return 0;
}

相关标签: