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

📅  最后修改于: 2021-04-24 17:47:59             🧑  作者: Mango

给定一个数组arr ,该数组代表一个圆形切割的不同角度,任务是确定所需的更多切割的最小数量,以便将圆形分成相等的部分。
注意:数组已经按升序排序。
例子:

方法:想法是计算在数组中两个元素的连续差值的情况下获得的所有值的最大公除数,以便为圆找到一个零件的最大可能尺寸(以减少所需的切割次数)。被分成。

  • 首先将数组的第一个两个值的绝对差存储在名为factor = arr [1] – arr [0]的变量中。
  • 现在遍历数组,从索引2N-1 ,对于每个元素更新因子factor = gcd(factor,arr [i] – arr [i-1])
  • 然后,对于最后一个元素,更新因子= 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