📌  相关文章
📜  由N个磁体形成的磁体组数

📅  最后修改于: 2021-04-29 16:55:37             🧑  作者: Mango

给定N个磁铁连续排成一排,要么左侧为负极,右侧为正极(01),要么为左侧为正极,右侧为负极(10)。考虑到以下事实:如果2个连续的磁铁彼此面对不同的磁极,则它们会形成一个组并相互吸引,请找出可能的组总数。

例子

Input : N = 6
        magnets = {10, 10, 10, 01, 10, 10}
Output : 3
The groups are formed by the following magnets:
1, 2, 3
4
5, 6

Input : N = 5
        magnets = {10, 10, 10, 10, 10, 01}
Output : 1

让我们考虑每对连续的磁体,有两种可能的情况:

  • 两者具有相同的配置:在这种情况下,连接端将具有不同的极点,因此它们将属于同一组。
  • 两者具有不同的配置:在这种情况下,连接端将具有相同的磁极,因此它们将彼此排斥以形成不同的组。

因此,只有在两个连续磁体具有不同配置的情况下,才会形成一个新的组。因此,遍历磁体阵列并找到具有不同配置的连续对的数量。

下面是上述方法的实现:

C++
// C++ program to find number of groups
// of magnets formed from N magnets
  
#include 
using namespace std;
  
// Function to count number of groups of
// magnets formed from N magnets
int countGroups(int n, string m[])
{
    // Intinially only a single group
    // for the first magnet
    int count = 1;
  
    for (int i = 1; i < n; i++)
  
        // Different configuration increases
        // number of groups by 1
        if (m[i] != m[i - 1])
            count++;
  
    return count;
}
  
// Driver Code
int main()
{
    int n = 6;
  
    string m[n] = { "10", "10", "10", "01", "10", "10" };
  
    cout << countGroups(n, m);
  
    return 0;
}


Java
// Java program to find the maximum number 
// of elements that can be added to a set 
// such that it is the absolute difference // of magnets formed from N magnets 
  
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG{  
      
// Function to count number of groups of 
// magnets formed from N magnets 
static int countGroups(int n, String m[]) 
{ 
    // Intinially only a single group 
    // for the first magnet 
    int count = 1; 
    
    for (int i = 1; i < n; i++) 
    
        // Different configuration increases 
        // number of groups by 1 
        if (m[i] != m[i - 1]) 
            count++; 
    
    return count; 
} 
    
// Driver Code 
public static void main(String args[]) 
{ 
    int n = 6; 
    
    String []m = { "10", "10", "10", "01", "10", "10" }; 
    
    System.out.println( countGroups(n, m)); 
    
}
}


Python 3
# Python 3 program to find number 
# of groups of magnets formed 
# from N magnets
  
# Function to count number of 
# groups of magnets formed
# from N magnets
def countGroups(n, m):
  
    # Intinially only a single 
    # group for the first magnet
    count = 1
  
    for i in range(1, n):
  
        # Different configuration increases
        # number of groups by 1
        if (m[i] != m[i - 1]):
            count += 1
  
    return count
  
# Driver Code
if __name__ == "__main__":
  
    n = 6
  
    m = [ "10", "10", "10", 
          "01", "10", "10" ]
  
    print(countGroups(n, m))
  
# This code is contributed
# by ChitraNayal


C#
// C# program to find number of groups
// of magnets formed from N magnets
using System;
  
class GFG {
  
    // Function to count number of groups of 
    // magnets formed from N magnets 
    static int countGroups(int n, String []m) 
    { 
          
        // Intinially only a single group 
        // for the first magnet 
        int count = 1; 
      
        for (int i = 1; i < n; i++) 
      
            // Different configuration increases 
            // number of groups by 1 
            if (m[i] != m[i - 1]) 
                count++; 
      
        return count; 
} 
  
// Driver Code
public static void Main()
{
    int n = 6; 
    String [] m = {"10", "10", "10",
                    "01", "10", "10"};
  
    Console.WriteLine(countGroups(n, m));
}
}
  
// This code is contributed by ANKITRAI1


PHP


输出:
3

时间复杂度: O(N)