檢查兩個二進位制字串是否可以透過交換 0 之前出現的 1 來使其相等
在本文中,我們將討論一個與字串操作和 C++ 中二進位制數相關的有趣問題。我們將要解決的問題是“檢查兩個二進位制字串是否可以透過交換 0 之前出現的 1 來使其相等”。這個問題是增強您對字串、二進位制數和演算法思維理解的絕佳方法。
問題陳述
任務是確定兩個二進位制字串是否可以透過交換字串中 0 之前出現的 1 來使其相等。
C++ 解決方案方法
解決此問題的方法是跟蹤兩個字串中 1 和 0 的數量。當且僅當兩個字串具有相同數量的 1 和 0 時,兩個二進位制字串可以透過交換 0 之前的 1 來使其相等。
示例
以下是實現此解決方案的程式 -
#include <stdio.h> #include <string.h> int canBeMadeEqual(char *str1, char *str2) { int count1s_str1 = 0, count0s_str1 = 0; int count1s_str2 = 0, count0s_str2 = 0; // Count the number of '1's and '0's in str1 for (int i = 0; str1[i] != '\0'; i++) { if (str1[i] == '1') { count1s_str1++; } else { count0s_str1++; } } // Count the number of '1's and '0's in str2 for (int i = 0; str2[i] != '\0'; i++) { if (str2[i] == '1') { count1s_str2++; } else { count0s_str2++; } } // Compare the counts of '1's and '0's for both strings return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2; } int main() { char str1[] = "1100"; char str2[] = "1010"; int result = canBeMadeEqual(str1, str2); // Print the result based on whether the strings can be made equal or not printf(result ? "The binary strings can be made equal.\n" : "The binary strings cannot be made equal.\n"); return 0; }
輸出
The binary strings can be made equal.
#include <iostream> #include <string> using namespace std; bool canBeMadeEqual(string str1, string str2) { int count1s_str1 = 0, count0s_str1 = 0; int count1s_str2 = 0, count0s_str2 = 0; for (char c : str1) { if (c == '1') { count1s_str1++; } else { count0s_str1++; } } for (char c : str2) { if (c == '1') { count1s_str2++; } else { count0s_str2++; } } return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2; } int main() { string str1 = "1100"; string str2 = "1010"; bool result = canBeMadeEqual(str1, str2); cout << (result ? "The binary strings can be made equal." : "The binary strings cannot be made equal.") << endl; return 0; }
輸出
The binary strings can be made equal.
public class BinaryStringEquality { public static boolean canBeMadeEqual(String str1, String str2) { int count1s_str1 = 0, count0s_str1 = 0; int count1s_str2 = 0, count0s_str2 = 0; // Count the number of '1's and '0's in str1 for (char c : str1.toCharArray()) { if (c == '1') { count1s_str1++; } else { count0s_str1++; } } // Count the number of '1's and '0's in str2 for (char c : str2.toCharArray()) { if (c == '1') { count1s_str2++; } else { count0s_str2++; } } // Compare the counts of '1's and '0's for both strings return count1s_str1 == count1s_str2 && count0s_str1 == count0s_str2; } public static void main(String[] args) { String str1 = "1100"; String str2 = "1010"; boolean result = canBeMadeEqual(str1, str2); // Print the result based on whether the strings can be made equal or not System.out.println(result ? "The binary strings can be made equal." : "The binary strings cannot be made equal."); } }
輸出
The binary strings can be made equal.
def can_be_made_equal(str1, str2): count1s_str1 = str1.count('1') count0s_str1 = str1.count('0') count1s_str2 = str2.count('1') count0s_str2 = str2.count('0') # Compare the counts of '1's and '0's for both strings return count1s_str1 == count1s_str2 and count0s_str1 == count0s_str2 def main(): str1 = "1100" str2 = "1010" result = can_be_made_equal(str1, str2) # Print the result based on whether the strings can be made equal or not print("The binary strings can be made equal." if result else "The binary strings cannot be made equal.") if __name__ == "__main__": main()
輸出
The binary strings can be made equal.
帶測試用例的說明
讓我們考慮二進位制字串“1100”和“1010”。
當我們將這些字串傳遞給 canBeMadeEqual 函式時,它會計算兩個字串中 1 和 0 的數量。
在字串“1100”中,有兩個 1 和兩個 0。在字串“1010”中,也有兩個 1 和兩個 0。
由於兩個字串中 1 和 0 的數量相等,因此該函式返回 true,表明可以透過交換 0 之前的 1 來使字串相等。
結論
此問題提供了一種絕佳的方式,可以應用字串操作和二進位制數的知識來解決 C++ 中的複雜問題。這是一個練習您的 C++ 編碼技能並瞭解如何在字串格式中處理二進位制數的極佳問題。
廣告