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

Count pairs with Odd XOR in C++: นับคู่ที่ XOR ได้ค่าเลขคี่

กำหนด array หาจำนวนคู่ (i,j) ที่ i < j และ arr[i] XOR arr[j] เป็นเลขคี่

ข้อสังเกต: XOR เป็นคี่ ⟺ หนึ่งตัวคู่ อีกตัวคี่

ตัวอย่าง: {2,8,1,5,11} → Even: 2,8 (2 ตัว), Odd: 1,5,11 (3 ตัว) → คู่ = 2×3 = 6

วิธีแก้ปัญหา

  1. นับจำนวนเลขคู่ (even) และเลขคี่ (odd)
  2. คำตอบ = even * odd

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

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

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

Input

{2,8,1,5,11}

Output

6