📌  相关文章
📜  将圆分成相等部分所需的最小切割

📅  最后修改于: 2021-10-23 08:29:29             🧑  作者: Mango

给定一个数组arr ,它表示一个圆被切割的不同角度,任务是确定所需的最少切割次数,以便将圆分成相等的部分。
注意:数组已按升序排序。
例子:

方法:这个想法是计算数组中两个元素的连续差值所获得的所有值的最大公约数,以便找到圆可以成为的部分的最大(以减少所需的切割次数)可能的大小被分成。

  • 首先将数组的第一个两个值的绝对差存储在名为factor = arr[1] – arr[0]的变量中。
  • 现在遍历数组从索引2N-1,并且对于每个元素更新因子factor = gcd(factor, arr[i] – arr[i-1])
  • 然后对于最后一个元素更新factor = gcd(factor, 360 – arr[N-1] + arr[0])
  • 最后,所需的总削减量为(360 / 因子) – N。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to return the number of cuts
// required to divide a circle into equal parts
int Parts(int Arr[], int N)
{
    int factor = Arr[1] - Arr[0];
    for (int i = 2; i < N; i++) {
        factor = __gcd(factor, Arr[i] - Arr[i - 1]);
    }
 
    // Since last part is connected with the first
    factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
 
    int cuts = (360 / factor) - N;
 
    return cuts;
}
 
// Driver code
int main()
{
    int Arr[] = { 0, 1 };
    int N = sizeof(Arr) / sizeof(Arr[0]);
 
    cout << Parts(Arr, N);
    return 0;
}


Java
// Java implementation of above approach
 
import java.io.*;
 
 
class GFG {
    // Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0)
          return b;
        if (b == 0)
          return a;
        
        // base case
        if (a == b)
            return a;
        
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
        return __gcd(a, b-a);
    }
      
 
// Function to return the number of cuts
// required to divide a circle into equal parts
static int Parts(int Arr[], int N)
{
    int factor = Arr[1] - Arr[0];
    for (int i = 2; i < N; i++) {
        factor = __gcd(factor, Arr[i] - Arr[i - 1]);
    }
 
    // Since last part is connected with the first
    factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
 
    int cuts = (360 / factor) - N;
 
    return cuts;
}
 
// Driver code
 
    public static void main (String[] args) {
    int Arr[] = { 0, 1 };
    int N = Arr.length;
 
    System.out.println( Parts(Arr, N));
    }
}
// This code is contributed by anuj_67..


Python 3
# Python 3 implementation of
# above approach
import math
 
# Function to return the number
# of cuts required to divide a
# circle into equal parts
def Parts(Arr, N):
 
    factor = Arr[1] - Arr[0]
    for i in range(2, N) :
        factor = math.gcd(factor, Arr[i] -
                                  Arr[i - 1])
     
    # Since last part is connected
    # with the first
    factor = math.gcd(factor, 360 -
                      Arr[N - 1] + Arr[0])
 
    cuts = (360 // factor) - N
 
    return cuts
 
# Driver code
if __name__ == "__main__":
    Arr = [ 0, 1 ]
    N = len(Arr)
 
    print( Parts(Arr, N))
 
# This code is contributed
# by ChitraNayal


C#
//  C# implementation of above approach
 
using System;
 
class GFG
{
   // Recursive function to return gcd of a and b
    static int __gcd(int a, int b)
    {
        // Everything divides 0 
        if (a == 0)
          return b;
        if (b == 0)
          return a;
        
        // base case
        if (a == b)
            return a;
        
        // a is greater
        if (a > b)
            return __gcd(a-b, b);
        return __gcd(a, b-a);
    }
      
 
    // Function to return the number of cuts
    // required to divide a circle into equal parts
    static int Parts(int []Arr, int N)
    {
        int factor = Arr[1] - Arr[0];
        for (int i = 2; i < N; i++) {
            factor = __gcd(factor, Arr[i] - Arr[i - 1]);
        }
     
        // Since last part is connected with the first
        factor = __gcd(factor, 360 - Arr[N - 1] + Arr[0]);
     
        int cuts = (360 / factor) - N;
     
        return cuts;
    }
 
    // Driver code
    static void Main()
    {
            int []Arr = { 0, 1 };
            int N = Arr.Length;
            Console.WriteLine(Parts(Arr, N));
    }
}
// This code is contributed by ANKITRAI1


PHP
 $b)
        return __gcd($a - $b, $b);
    return __gcd($a, $b - $a);
}
 
// Function to return the number of cuts
function Parts($Arr, $N)
{
    $factor = $Arr[1] - $Arr[0];
    for ($i = 2; $i < $N; $i++)
    {
        $factor = __gcd($factor, $Arr[$i] -
                                 $Arr[$i - 1]);
    }
 
    // Since last part is connected
    // with the first
    $factor = __gcd($factor, 360 -
                    $Arr[$N - 1] + $Arr[0]);
 
    $cuts = (360 / $factor) - $N;
 
    return $cuts;
}
 
// Driver code
$Arr = array( 0, 1 );
$N = sizeof($Arr);
echo (Parts($Arr, $N));
 
// This code is contributed by ajit.
?>


Javascript


输出:
358

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程