C++输出布尔序列对应整数值及数组连续升序子序列内容
Got it, let's tackle these two C++ problems one by one:
First, let's clarify the core logic: we’ll treat true as binary digit 1 and false as 0. The key callout here is deciding the bit order—whether the first element in your sequence is the most significant bit (MSB) or least significant bit (LSB). I’ve covered both common scenarios below.
Example Breakdown
Suppose your boolean sequence is [true, false, true, true]:
- If first element = MSB: Translates to binary
1011→ decimal11 - If first element = LSB: Translates to binary
1101→ decimal13
Code Implementation
#include <iostream> #include <vector> // Convert boolean sequence to integer (first element = MSB) int boolSeqToIntMSB(const std::vector<bool>& seq) { int result = 0; for (bool b : seq) { result = (result << 1) | (b ? 1 : 0); } return result; } // Convert boolean sequence to integer (first element = LSB) int boolSeqToIntLSB(const std::vector<bool>& seq) { int result = 0; int bitPosition = 0; for (bool b : seq) { if (b) { result |= (1 << bitPosition); } bitPosition++; } return result; } int main() { std::vector<bool> exampleSeq = {true, false, true, true}; std::cout << "MSB-first result: " << boolSeqToIntMSB(exampleSeq) << std::endl; std::cout << "LSB-first result: " << boolSeqToIntLSB(exampleSeq) << std::endl; return 0; }
Quick Explanation
- MSB-first approach: Shift the current result left by 1 bit (equivalent to multiplying by 2) and OR it with the current boolean’s binary value to build the integer from highest to lowest bit.
- LSB-first approach: Calculate the value of each bit using
1 << bitPositionand add it to the result if the boolean istrue, building from lowest to highest bit.
Your original function counts 4-element consecutive increasing subsequences (where each element is exactly 1 more than the previous, e.g., 23,24,25,26). To switch from counting to outputting the actual subsequences, we just replace the counter increment with logic that prints or stores the matching elements.
Key Assumptions
- We’re working with an integer array of length 20.
- A valid subsequence means 4 consecutive indices where
arr[i+1] = arr[i]+1,arr[i+2] = arr[i+1]+1, andarr[i+3] = arr[i+2]+1.
Modified Code (Print Directly)
#include <iostream> // Output all valid 4-element consecutive increasing subsequences void printValidSubsequences(const int arr[], int size) { // We can only start checking up to index size-4 (needs 4 elements total) for (int i = 0; i <= size - 4; ++i) { // Check if the 4 elements meet the "consecutive +1" rule if (arr[i+1] == arr[i] + 1 && arr[i+2] == arr[i+1] + 1 && arr[i+3] == arr[i+2] + 1) { // Print the matching subsequence std::cout << "Valid subsequence: " << arr[i] << ", " << arr[i+1] << ", " << arr[i+2] << ", " << arr[i+3] << std::endl; } } } int main() { int arr[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; printValidSubsequences(arr, 20); return 0; }
Alternative: Store Subsequences First
If you want to collect the subsequences before outputting (e.g., for later processing), use this variant:
#include <iostream> #include <vector> // Collect all valid subsequences into a vector std::vector<std::vector<int>> getValidSubsequences(const int arr[], int size) { std::vector<std::vector<int>> result; for (int i = 0; i <= size - 4; ++i) { if (arr[i+1] == arr[i] + 1 && arr[i+2] == arr[i+1] + 1 && arr[i+3] == arr[i+2] + 1) { result.push_back({arr[i], arr[i+1], arr[i+2], arr[i+3]}); } } return result; } int main() { int arr[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; auto validSubs = getValidSubsequences(arr, 20); // Print the collected subsequences std::cout << "Found " << validSubs.size() << " valid subsequences:\n"; for (const auto& sub : validSubs) { std::cout << "- "; for (int num : sub) { std::cout << num << " "; } std::cout << "\n"; } return 0; }
Explanation
- We loop through the array starting at index 0, stopping at
size-4to ensure we always have 4 elements left to check. - For each starting index, we verify the consecutive +1 condition. When a match is found, we either print it immediately or add it to a vector for later use.
内容的提问来源于stack exchange,提问作者Uncle Drew

