You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++输出布尔序列对应整数值及数组连续升序子序列内容

Got it, let's tackle these two C++ problems one by one:

Problem 1: Convert a Boolean Sequence to Its Corresponding Integer Value in C++

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 → decimal 11
  • If first element = LSB: Translates to binary 1101 → decimal 13

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 << bitPosition and add it to the result if the boolean is true, building from lowest to highest bit.

Problem 2: Modify Function to Output Valid Subsequences Instead of Count

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, and arr[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-4 to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:27:40