📜  计算连续的零点对

📅  最后修改于: 2021-06-25 16:39:31             🧑  作者: Mango

考虑在计算机上以1开头的序列。在每个后续步骤中,机器同时将每个数字0转换为序列10,并将每个数字1转换为序列01。
在第一个时间步骤之后,获得序列01;在第二个之后是序列1001,在第三个之后是序列01101001,依此类推。

n个步骤后,序列中会出现几对连续的零?

例子 :

Input : Number of steps = 3
Output: 1
// After 3rd step sequence will be  01101001

Input : Number of steps = 4
Output: 3
// After 4rd step sequence will be 1001011001101001

Input : Number of steps = 5
Output: 5
// After 3rd step sequence will be  01101001100101101001011001101001

这是一个简单的推理问题。如果我们非常仔细地看到序列,那么我们将能够找到给定序列的模式。如果n = 1序列为{01},那么连续零的对数为0,如果n = 2序列为{1001},那么连续零的对数为1,如果n = 3序列为{01101001}所以连续零对的数目是1
如果n = 4,则序列为{1001011001101001},因此连续零对的对数为3。

因此,序列的长度始终是2的幂。我们可以看到长度为12的序列在重复后的长度为12。在长度为12的段中,总共有2对连续的零。因此,我们可以概括给定的模式q =(2 ^ n / 12),并且连续零的总对将为2 * q + 1。

C++
// C++ program to find number of consecutive
// 0s in a sequence
#include
using namespace std;
  
// Function to find number of consecutive Zero Pairs
// Here n is number of steps
int consecutiveZeroPairs(int n)
{
    // Base cases
    if (n==1)
        return 0;
    if (n==2 || n==3)
        return 1;
  
    // Calculating how many times divisible by 12, i.e.,
    // count total number repeating segments of length 12
    int q = (pow(2, n) / 12);
  
    // number of consecutive Zero Pairs
    return 2 * q + 1;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << consecutiveZeroPairs(n) << endl;
    return 0;
}


Java
//Java program to find number of 
// consecutive 0s in a sequence
import java.io.*;
import java.math.*;
  
class GFG {
      
    // Function to find number of consecutive
    // Zero Pairs. Here n is number of steps
    static int consecutiveZeroPairs(int n)
    {
        // Base cases
        if (n == 1)
            return 0;
        if (n == 2 || n == 3)
            return 1;
  
        // Calculating how many times divisible
        // by 12, i.e.,count total number 
        // repeating segments of length 12
        int q = ((int)(Math.pow(2, n)) / 12);
  
        // number of consecutive Zero Pairs
        return (2 * q + 1);
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        System.out.println(consecutiveZeroPairs(n));
    }
}
      
// This code is contributed by Nikita Tiwari.


Python
# Python program to find number of 
# consecutive 0s in a sequence
import math
  
# Function to find number of consecutive
# Zero Pairs. Here n is number of steps
def consecutiveZeroPairs(n) :
  
    # Base cases
    if (n == 1) :
        return 0
    if (n == 2 or n == 3) :
        return 1
  
    # Calculating how many times divisible
    # by 12, i.e.,count total number 
    # repeating segments of length 12
    q =(int) (pow(2,n) / 12)
  
    # number of consecutive Zero Pairs
    return 2 * q + 1
  
# Driver code
n = 5
print consecutiveZeroPairs(n)
  
#This code is contributed by Nikita Tiwari.


C#
// C# program to find number of 
// consecutive 0s in a sequence
using System;
  
class GFG {
      
    // Function to find number of 
    // consecutive Zero Pairs.
    // Here n is number of steps
    static int consecutiveZeroPairs(int n)
    {
        // Base cases
        if (n == 1)
            return 0;
        if (n == 2 || n == 3)
            return 1;
  
        // Calculating how many times divisible
        // by 12, i.e.,count total number 
        // repeating segments of length 12
        int q = ((int)(Math.Pow(2, n)) / 12);
  
        // number of consecutive Zero Pairs
        return (2 * q + 1);
    }
  
    // Driver Code
    public static void Main()
    {
        int n = 5;
        Console.Write(consecutiveZeroPairs(n));
    }
}
      
// This code is contributed by Nitin mittal.


PHP


输出 :

5