Задать вопрос
Ответы пользователя по тегу Математика
  • Как решать задачу на булеву логику?

    @Xiran Автор вопроса
    Написал код
    Код

    #include <cassert>
    #include <iostream>
    
    constexpr std::uint8_t THREE_BITS_MAX = 7, THREE_ARY_BOOL_FN_MAX = 255;
    
    inline bool get_nth_bit(int reg, int x) {
        return (reg >> x) & 1;
    }
    
    template<std::size_t M>
    class iteratable {
    public:
        inline void next() {
            if (raw_ == M) {
                is_end_ = true;
                return;
            }
    
            raw_++;
        }
    
        inline bool is_end() const {
            return is_end_;
        }
    protected:
        std::uint8_t raw_ { 0 };
        bool is_end_ { 0 };
    };
    
    class three_bits_pack : public iteratable<THREE_BITS_MAX> {
    public:
        inline bool operator[](std::size_t index) const {
            assert(index <= 3);
    
            return get_nth_bit(raw_, index);
        }
    
        inline auto raw() const {
            return raw_;
        }
    };
    
    
    class three_ary_bool_fn : public iteratable<THREE_ARY_BOOL_FN_MAX> {
    public:
        inline bool operator()(const three_bits_pack &pack) const {
            return get_nth_bit(raw_, pack.raw());
        }
    };
    
    int main() {
        int counter = 0;
    
        for (three_ary_bool_fn a_fn; !a_fn.is_end(); a_fn.next()) {
            for (three_ary_bool_fn b_fn; !b_fn.is_end(); b_fn.next()) {
                for (three_bits_pack abc; !abc.is_end(); abc.next()) {
                    if (a_fn(abc) && !b_fn(abc))
                        goto next;
    
                    for (three_bits_pack def; !def.is_end(); def.next()) {
                        if (!(!abc[0] || !abc[1] || !abc[2] || a_fn(def)))
                            goto next;
    
                        if (!(def[0] || def[1] || def[2] || b_fn(abc)))
                            goto next;
                    }
                }
                counter++;
    next:;
            }
        }
    
        std::cout << counter;
    }



    "Трюк" основан на том, что любую логическую 3-арную функцию можно представить как 8 битов, где номер каждого бита равен комбинации битов, которые от этой функции дают этот самый бит.
    Только вот ничего не работает как надо. Выдает 1 и все, а должно 5103.
    Ответ написан