
这里我们会看到一个问题。我们有一个二进制数组。它有n个元素。每个元素要么是 0,要么是 1。最初,所有元素都是 0。现在我们将提供 M 命令。每个命令将包含开始和结束索引。所以 command(a, b) 表示该命令将从位置 a 的元素应用到位置 b 的元素。该命令将切换值。所以它会从 ath 索引切换到 bth 索引。这个问题很简单。检查算法以获得概念。
算法
toggleCommand(arr, a, b)
Begin
for each element e from index a to b, do
toggle the e and place into arr at its position.
done
End示例
#includeusing namespace std; void toggleCommand(int arr[], int a, int b){ for(int i = a; i <= b; i++){ arr[i] ^= 1; //toggle each bit in range a to b } } void display(int arr[], int n){ for(int i = 0; i 输出
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0










