检查给定数字的二进制表示中是否只包含“01”和“10”作为子字符串
在本文中,我们将深入探讨二进制字符串操作领域的一个有趣问题:“检查给定数字的二进制表示中是否只包含'01'和'10'作为子字符串”。这个问题挑战我们验证一个数字的二进制表示是否只包含子字符串'01'和'10'。我们将详细讨论这个问题,提供C++代码实现,并用示例说明这个概念。
理解问题陈述
给定一个数字,任务是检查其二进制表示是否只包含'01'和'10'作为子字符串。换句话说,我们需要验证给定数字的二进制表示是否由交替的0和1组成。
方法
解决这个问题的一个简单方法是将数字转换为其二进制表示,然后检查二进制字符串是否包含任何'00'或'11'子字符串。如果包含,则二进制表示不只包含'01'和'10'子字符串。如果不包含,则包含。
示例
以下是上述方法的程序:
#include <stdio.h> int checkBinary(int n) { char binary[33]; sprintf(binary, "%d", n); // Convert integer to string for (int i = 0; binary[i + 1] != '\0'; i++) { if (binary[i] == binary[i + 1]) { return 0; // Binary representation contains consecutive same digits } } return 1; // Binary representation contains only alternating '0's and '1's } int main() { int n = 5; if (checkBinary(n)) { printf("The binary representation of %d contains only '01' and '10' substrings.\n", n); } else { printf("The binary representation of %d does not contain only '01' and '10' substrings.\n", n); } return 0; }
输出
The binary representation of 5 contains only '01' and '10' substrings.
#include<bits/stdc++.h> using namespace std; bool checkBinary(int n) { string binary = bitset<32>(n).to_string(); return binary.find("00") == string::npos && binary.find("11") == string::npos; } int main() { int n = 5; if (checkBinary(n)) { cout << "The binary representation of " << n << " contains only '01' and '10' substrings."; } else { cout << "The binary representation of " << n << " does not contain only '01' and '10' substrings."; } return 0; }
输出
The binary representation of 5 contains only '01' and '10' substrings.
public class BinarySubstringCheck { public static boolean checkBinary(int n) { String binary = Integer.toBinaryString(n); for (int i = 0; i < binary.length() - 1; i++) { if (binary.charAt(i) == binary.charAt(i + 1)) { return false; // Binary representation contains consecutive same digits } } return true; // Binary representation contains only alternating '0's and '1's } public static void main(String[] args) { int n = 5; if (checkBinary(n)) { System.out.println("The binary representation of " + n + " contains only '01' and '10' substrings."); } else { System.out.println("The binary representation of " + n + " does not contain only '01' and '10' substrings."); } } }
输出
The binary representation of 5 contains only '01' and '10' substrings.
def check_binary(n): binary = bin(n)[2:] # Convert integer to binary string for i in range(len(binary) - 1): if binary[i] == binary[i + 1]: return False # Binary representation contains consecutive same digits return True # Binary representation contains only alternating '0's and '1's def main(): n = 5 if check_binary(n): print(f"The binary representation of {n} contains only '01' and '10' substrings.") else: print(f"The binary representation of {n} does not contain only '01' and '10' substrings.") if __name__ == "__main__": main()
输出
The binary representation of 5 contains only '01' and '10' substrings.
这段代码首先使用bitset函数将数字转换为其二进制表示。然后,它使用find函数检查此二进制字符串是否包含任何'00'或'11'子字符串。
测试用例
让我们考虑数字5。它的二进制表示是'101'。正如你所看到的,它只包含'01'和'10'作为子字符串。因此,代码的输出将是:“数字5的二进制表示只包含'01'和'10'子字符串。”
结论
本文深入探讨了检查给定数字的二进制表示是否只包含'01'和'10'子字符串的问题。这个问题虽然看起来很复杂,但在有条理地处理时会变得简单。我们讨论了解决这个问题的策略,用C++实现了该方法,并用一个实际示例解释了这个概念。
广告