Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Count pairs with Bitwise OR as Even number in C++

นับคู่ที่ a | b เป็นเลขคู่

ข้อสังเกต: a | b เป็นคู่ ⟺ a และ b เป็นคู่ทั้งคู่ (LSB = 0)

คำตอบ = C(even_count, 2)

ตัวอย่าง: {2,5,1,8,9,2,7} → Even: 2,8,2 (3 ตัว) → C(3,2) = 3

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;

int count_pair(int arr[], int n) {
    int even = 0;
    for(int i=0;i

Input

{2,5,1,8,9,2,7}

Output

3