檢查給定數字的二進位制表示中是否只包含“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++實現了該方法,並用一個實際示例解釋了這個概念。
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP