CCF CSP练习 202009-2

本文探讨了两段不同的C++代码在解决同一问题时的错误理解与正解。第一段代码由于对题目理解错误,导致计算结果不准确,错误地将经过和逗留人数相加等于总人数。而第二段正解代码则正确地区分了经过和逗留,只计算了在特定区域内逗留超过指定时间的人数。文章通过这个例子强调了仔细审题和正确理解问题的重要性。
摘要由CSDN通过智能技术生成

题目链接http://118.190.20.162/view.page?gpid=T112

记录自己没仔细审题,加上样例输出结果产生的错觉。

错误理解:误以为经过人数+逗留人数=n

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

typedef struct {
	int x;
	int y;
}Point;

int main()
{
	int n,k,t,xl,yd,xr,yu;
	vector<int> ans;//标记逗留人数 
	vector<int> point[20+1]; 
	cin >> n >> k >> t >> xl >> yd >> xr >> yu;
	for(int i=0;i<n;i++){
		for(int j=0;j<t;j++){
			Point p;
			cin >> p.x >> p.y;
			if(p.x>=xl&&p.x<=xr&&p.y>=yd&&p.y<=yu){
				point[i].push_back(1);
			}else{
				point[i].push_back(0);
			}
		}
	}
//	for(int i=0;i<n;i++){
//		cout << "the " << i << endl; 
//		for(int j=0;j<t;j++){
//			cout << point[i][j] << endl;
//		}
//	}
		
	for(int i=0;i<n;i++){
		int cnt=0;//标记是否连续 >k 
		int sum=0;
		//cout << "the " << i << endl;
		for(int j=0;j<t;j++){
			if(point[i][j]==1){
				cnt++;
				if(cnt>=k){
					ans.push_back(1);
					break;
				}
			}
			else cnt=0;
			if(j==t-1){
				ans.push_back(0);
			}
//			if(cnt=1){
//				sum+=1;
//				cout << "sum: " << sum << endl;
//				if(sum>=k){
//					ans.push_back(1);
//					break;
//				} 
//			} 
//			else sum=0;
//			//cout << "::" << cnt << endl;
//			if(point[i][j]==1){
//				cnt=1;
//			}else cnt=0;
//			if(j==t-1){
//				if(cnt==1){
//					if(sum+1>=k) ans.push_back(1);
//				} 
//				else ans.push_back(0);
//			} 
		}
	} 
	
//	for(int i=0;i<n;i++){
//		cout << ans[i] << endl;
//	}
	int across=count(ans.begin(),ans.end(),1);
	cout << n-across << endl;
	cout << across << endl;
	
	return 0;
}

正解:经过和逗留分别计数,也就是在经过的基础上才有逗留,而非(逗留+经过)=人口总数n

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;

typedef struct {
	int x;
	int y;
}Point;

int main()
{
	int n,k,t,xl,yd,xr,yu;
	int across=0,stay=0;
	cin >> n >> k >> t >> xl >> yd >> xr >> yu;
	for(int i=0;i<n;i++){
		int cnt=0;
		int _max=0;
		int flag=0;
		for(int j=0;j<t;j++){
			int x,y;
			cin >> x >> y;
			if(x>=xl&&x<=xr&&y>=yd&&y<=yu){
				cnt++;
				flag=1;
				if(cnt>_max){
					_max=cnt;
				}
			}
			else cnt=0;
		}
		if(flag) across++;
		if(_max>=k) stay++;
	
	}

	cout << across << endl << stay << endl;	
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值