202009-2 风险人群筛查

文章讲述了在C++代码中,如何通过结构体`Point`和`flag`变量来记录人员进入风险区的情况,原始代码存在bug,修正后的代码确保了每次进入风险区后flag正确更新。
摘要由CSDN通过智能技术生成

C++:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
    int x, y;
}p[N];
int main(){
    int n, k, t, xl, xr, yl, yh;
    scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
    
    int res1 = 0, res2 = 0;
    while(n--){
        int cnt = 0, cnt1 = 0;
        for(int i = 1; i <= t; i++){
            //cout << "cnt1:" << cnt1 << endl;
            scanf("%d%d", &p[i].x, &p[i].y);
        }
        for(int i = 1; i <= t; i++){
            if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
                cnt++;
                cnt1++;
                if(cnt1 >= k){
                    res2++;
                    break;
                } 
            }else{
                cnt1 = 0;
            }
        }
        if(cnt) res1++;
    }
    cout << res1 << endl;
    cout << res2 << endl;
}

错误代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
    int x, y;
    bool flag = false;
}p[N];
int main(){
    int n, k, t, xl, xr, yl, yh;
    scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
    
    int res1 = 0, res2 = 0;
    while(n--){
        int cnt = 0, cnt1 = 0;
        for(int i = 1; i <= t; i++){
            //cout << "cnt1:" << cnt1 << endl;
            scanf("%d%d", &p[i].x, &p[i].y);
        }
        for(int i = 1; i <= t; i++){
            if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
                p[i].flag = true;
                cnt++;
                
                if(p[i - 1].flag) cnt1++;
                else cnt1 = 1;
                if(cnt1 >= k){
                    res2++;
                    break;
                } 
            }
            
        }
        if(cnt) res1++;
    }
    cout << res1 << endl;
    cout << res2 << endl;
}

错误原因:用flag记录是否经过风险区,但是没有把没经过风险区的时刻设为false,这样当遍历到下一个人时,可能用的是前一个人前一时刻的flag。

改正代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
    int x, y;
    bool flag = false;
}p[N];
int main(){
    int n, k, t, xl, xr, yl, yh;
    scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
    
    int res1 = 0, res2 = 0;
    while(n--){
        int cnt = 0, cnt1 = 0;
        for(int i = 1; i <= t; i++){
            //cout << "cnt1:" << cnt1 << endl;
            scanf("%d%d", &p[i].x, &p[i].y);
        }
        for(int i = 1; i <= t; i++){
            if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
                p[i].flag = true;
                cnt++;
                
                if(p[i - 1].flag) cnt1++;
                else cnt1 = 1;
                if(cnt1 >= k){
                    res2++;
                    break;
                } 
            }else{
                p[i].flag = false;
            }
            
        }
        if(cnt) res1++;
    }
    cout << res1 << endl;
    cout << res2 << endl;
}

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值