zoukankan      html  css  js  c++  java
  • SDNU 1354.Cut the Cable(gcd)

    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
    14
    思路:这道题说是要切电缆,长度不能有浪费,输入的所有长度是整数,输出的结果长度也是个整数,那就是用gcd来求所有原始长度的最大公约数(让所有原始长度都可以整除,这样就不存在浪费),然后再求数量即可。(我个沙雕居然一开始想用二分,结果发现没法分)
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <map>
    using namespace std;
    #define ll long long
    
    const int inf = 0x3f3f3f3f;
    const int mod = 1e9+7;
    
    int n, a[10000+8], num, len;
    
    int gcd(int x, int y)
    {
        if(y != 0)return gcd(y, x%y);
        return x;
    }
    
    int main()
    {
        num = 0;
        scanf("%d", &n);
        for(int i = 0; i<n; i++)
            scanf("%d", &a[i]);
        len = a[0];
        for(int i = 1; i<n; i++)
            len = gcd(len, a[i]);
        for(int i = 0; i<n; i++)
            num += a[i]/len;
        printf("%d
    %d
    ", len, num);
        return 0;
    }
  • 相关阅读:
    C语言——enum
    C语言——杂实例
    pc上用C语言模拟51多任务的案例程序
    C语言结构体实例-创建兔子
    C语句模拟多任务实例
    求解1^2+2^2+3^2+4^2+...+n^2的方法(求解1平方加2平方加3平方...加n平方的和)
    啊啊啊哦哦
    2013多校联合3 G The Unsolvable Problem(hdu 4627)
    c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件
    揭开Socket编程的面纱
  • 原文地址:https://www.cnblogs.com/RootVount/p/11262751.html
Copyright © 2011-2022 走看看