POJ2943 UVALive3524 The Cow Doctor【bitset+枚举】

本文针对一个具体的编程问题进行探讨:在使用bitset时未初始化导致的逻辑错误。通过对比两段代码,作者发现bitset若不显式初始化则默认值可能影响程序逻辑,进而影响最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自 海岛Blog

转这篇文章主要是对19行的代码有点不明白,因为本人调试的代码,bitset不赋初值则位上的数都为0, 但是,把该段删除后WA,不明白。

/* POJ2943 UVALive3524 The Cow Doctor */
 
#include <iostream>
#include <bitset>
#include <math.h>
 
using namespace std;
 
int const N = 300;
bitset<N + 1> a[N + 1], b[N + 1];
 
int main()
{
    ios::sync_with_stdio(false);
 
    int n, m, k, v;
    while(cin >> n >> m && (n || m)) {
        for(int i = 0; i < m; i++)
            a[i] = 0, b[i] = 0;
 
        for(int i = 0; i < m; i++) {
            cin >> k;
            while(k--) {
                cin >> v;
                a[i].set(v);
            }
 
            for(int j = 0; j < i; j++)
                if((a[i] & a[j]) == a[i])
                    b[j] |= a[i];
                else if((a[i] & a[j]) == a[j])
                    b[i] |= a[j];
        }
 
        int cnt = 0;
        for(int i = 0; i < m; i++)
            if(a[i] == b[i])
                cnt++;
        cout << cnt << endl;
    }
 
    return 0;
}
 

以下为本人调试的代码

#include <iostream>
#include <bitset>
#include <math.h>

using namespace std;

int const N = 300;
bitset<N + 1> a[N + 1], b[N + 1];

int main()
{
    ios::sync_with_stdio(false);


        for(int i = 0; i < 200; i++)
         cout << a[i] << " " << b[i] << endl, a[i] = 0, b[i] = 0;
        cout << endl << endl << endl;
        for(int i = 0; i < 200; i++)
          cout << a[i] << " " << b[i] << endl;
    return 0;
}

结果是一样的,so,这个  晕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值