zoukankan      html  css  js  c++  java
  • [Aizu] ITP2_10_C~D: bit flag and bit mask

    前言

    ITP系列之位运算, 具体内容参见bitset

    题目链接

    ITP2_10_C: Bit Flag
    ITP2_10_D: Bit Mask

    求解

    第一题

    明确的指定了让处理哪一位, 很容易就用bitset类搞定了

    #include <bits/stdc++.h>
    using namespace std;
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        int q, com, i;
        bitset<64> bs;
        bs.reset();
     
        cin >> q;
        while (q--) {
            cin >> com;
            switch (com) {
                case 0:
                    cin >> i;
                    cout << bs.test(i) << endl;
                    break;
                case 1:
                    cin >> i;
                    bs.set(i);
                    break;
                case 2:
                    cin >> i;
                    bs.reset(i);
                    break;
                case 3:
                    cin >> i;
                    bs.flip(i);
                    break;
                case 4:
                    cout << (bs.all() ? 1 : 0) << endl;
                    break;
                case 5:
                    cout << (bs.any() ? 1 : 0) << endl;
                    break;
                case 6:
                    cout << (bs.none() ? 1 : 0) << endl;
                    break;
                case 7:
                    cout << bs.count() << endl;
                    break;
                case 8:
                    cout << bs.to_ulong() << endl;
            }
        }
    }
    

    第二题

    题目中说明了, 不再是给定一位进行处理, 而是给定一部分(掩码), 然后每次都要对这一部分进行对应的处理, 我第一次尝试的时候是通过创建一个嵌套的vector容器来存储那些特殊值的, 在看了别人的代码后, 才尝试着去使用bitset作为数组元素来用.

    第一次代码

    #include <bits/stdc++.h>
    using namespace std;
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        vector<vector<int> > vec;
        vector<int> ve;
        vector<int>::iterator it;
        bitset<64> bs;
        int n, q, com, i, k;
        int flag, cnt;
        unsigned long val;
     
        cin >> n;
        while (n--) {
            ve.clear();
            cin >> k;
            while (k--) {
                cin >> i;
                ve.push_back(i);
            }
            vec.push_back(ve);
        }
     
        cin >> q;
        while (q--) {
            cin >> com >> i;
            switch (com) {
                case 0:
                    cout << bs.test(i) << endl;
                    break;
                case 1:
                    ve = vec[i];
                    for (it = ve.begin(); it != ve.end(); it++) {
                        bs.set(*it);
                    }
                    break;
                case 2:
                    ve = vec[i];
                    for (it = ve.begin(); it != ve.end(); it++) {
                        bs.reset(*it);
                    }
                    break;
                case 3:
                    ve = vec[i];
                    for (it = ve.begin(); it != ve.end(); it++) {
                        bs.flip(*it);
                    }
                    break;
                case 4:
                    ve = vec[i];
                    flag = 1;
                    for (it = ve.begin(); flag && it != ve.end(); it++) {
                        flag = bs.test(*it);
                    }
                    cout << flag << endl;
                    break;
                case 5:
                    ve = vec[i];
                    flag = 0;
                    for (it = ve.begin(); flag == 0 && it != ve.end(); it++) {
                        flag = bs.test(*it);
                    }
                    cout << flag << endl;
                    break;
                case 6:
                    ve = vec[i];
                    flag = 0;
                    for (it = ve.begin(); flag == 0 && it != ve.end(); it++) {
                        flag = bs.test(*it);
                    }
                    if (flag) cout << 0 << endl;
                    else cout << 1 << endl;
                    break;
                case 7:
                    ve = vec[i];
                    cnt = 0;
                    for (it = ve.begin(); it != ve.end(); it++) {
                        cnt += bs.test(*it);
                    }
                    cout << cnt << endl;
                    break;
                case 8:
                    ve = vec[i];
                    val = 0;
                    for (it = ve.begin(); it != ve.end(); it++) {
                        val += ((unsigned long)bs.test(*it)) << *it;
                    }
                    cout << val << endl;
            }
        }
    }
    

    第二次学习别人风格的代码

    #include <bits/stdc++.h>
    using namespace std;
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        vector<bitset<64> > v(10);
        bitset<64> bs;
     
        int n, q, k, com, i;
     
        cin >> n;
        for (int j = 0; j < n; j++) {
            cin >> k;
            while (k--) {
                cin >> i;
                v[j][i] = 1;
            }
        }
     
        cin >> q;
        while (q--) {
            cin >> com >> i;
            if (com == 0) cout << bs.test(i) << endl;
            if (com == 1) bs |= v[i];
            if (com == 2) bs &= ~v[i];
            if (com == 3) bs ^= v[i];
            if (com == 4) cout << ((bs & v[i]) == v[i]) << endl;
            if (com == 5) cout << ((bs & v[i]).to_ulong() != 0) << endl;
            if (com == 6) cout << ((bs & v[i]).to_ulong() == 0) << endl;
            if (com == 7) cout << (bs & v[i]).count() << endl;
            if (com == 8) cout << (bs & v[i]).to_ulong() << endl;
        }
    }
    
  • 相关阅读:
    PHP 去除HTML标签 HTML实体转字符 br转
    PHP while使用
    js滚动条滚动到某个元素位置
    js按键监听
    用onerror处理图片获取失败问题
    windows 查看端口占用以及关闭该进程
    js获取当前时间戳 不需毫秒数
    escape,encodeURI,encodeURIComponent的区别
    php 获取中文长度 截取中文字符串
    将时间转换为xxx天前 xxx..前
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11025245.html
Copyright © 2011-2022 走看看