考虑一个从机器上的1开始的序列。在每个连续步骤中,机器同时将每个数字0转换为序列10,将每个数字1转换为序列01。 在第一时间步之后,获得序列01;第二个之后是序列1001,第三个之后是序列0110101,依此类推。 n步之后,序列中将出现多少对连续零? 例如:
null
Input : Number of steps = 3Output: 1// After 3rd step sequence will be 01101001Input : Number of steps = 4Output: 3// After 4rd step sequence will be 1001011001101001Input : Number of steps = 5Output: 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的一段中,总共有两对连续的零。因此,我们可以推广给定的模式q=(2^n/12),连续零的总对数为2*q+1。
C++
// C++ program to find number of consecutive // 0s in a sequence #include<bits/stdc++.h> 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. |
Python3
# 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
<?php // PHP program to find number // of consecutive 0s in a sequence // Function to find number // of consecutive Zero Pairs // Here n is number of steps function consecutiveZeroPairs( $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 $q = floor (pow(2, $n ) / 12); // number of consecutive Zero Pairs return 2 * $q + 1; } // Driver code $n = 5; echo consecutiveZeroPairs( $n ) ; // This code is contributed // by nitin mittal. ?> |
Javascript
<script> //javascript program to find number of // consecutive 0s in a sequence // Function to find number of consecutive // Zero Pairs. Here n is number of steps function consecutiveZeroPairs(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 var q =(parseInt((Math.pow(2, n)) / 12)); // number of consecutive Zero Pairs return (2 * q + 1); } // Driver code var n = 5; document.write(consecutiveZeroPairs(n)); // This code is contributed by umadevi9616 </script> |
输出:
5
本文由 沙申克·米什拉(古卢) .本文由Geeksforgeks团队审阅。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END