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

ตรวจสอบว่าเลขสามารถเขียนเป็น 2ˣ + 2ʸ ได้หรือไม่ ใน C++


ตรวจสอบว่า N = 2ˣ + 2ʸ โดย x,y > 0 (ซ้ำ ID 1981)

ตัวอย่าง: 10 = 2³ + 2¹

เงื่อนไข: N ต้องเป็นเลขคู่ (เพราะ 2ˣ + 2ʸ = 2 × (2ˣ⁻¹ + 2ʸ⁻¹))

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

#include <iostream>
using namespace std;

bool isSumofTwosPower(int n) {
    return ((n & 1) == 0);
}

int main() {
    int num = 86;
    if (isSumofTwosPower(num)) cout << "Can be represented";
    else cout << "Cannot be represented";
}

ผลลัพธ์

Can be represented