CCF CSP 回收站选址 C++

回收站选址

在这里插入图片描述
定义结构体存放坐标和判断是否为垃圾站的变量和得分变量,输入所有坐标,刷新结构体内变量的值,最后通过nearRubbish和score判断是否为垃圾站并输出不同得分的垃圾站数目

#include<bits/stdc++.h>
using namespace std;
struct Rubbish{ //定义一个结构体,x,y,存放坐标,nearRubbish记录上下左右的垃圾数,score记录对角的垃圾数 
	int x;
	int y;
	int nearRubbish = 0;
	int score = 0;
}rubbish[1001];

int main(){
	int n;
	cin>>n;
	int scoreKind[5] = {0};//存放得分为0-4的回收站数 
	for(int i = 0; i < n; i++){
		cin>>rubbish[i].x>>rubbish[i].y;
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			if(rubbish[i].x == rubbish[j].x&&rubbish[i].y==rubbish[j].y+1){  
			//判断是否为回收站,如果上下左右都有垃圾那么nearRubbish在 退出循环时的值为4,即符合回收站的标准 
				rubbish[i].nearRubbish++;
			}
			else if(rubbish[i].x == rubbish[j].x&&rubbish[i].y==rubbish[j].y-1){
				rubbish[i].nearRubbish++;
			}
			else if(rubbish[i].x == rubbish[j].x-1&&rubbish[i].y==rubbish[j].y){
				rubbish[i].nearRubbish++;
			}
			else if(rubbish[i].x == rubbish[j].x+1&&rubbish[i].y==rubbish[j].y){
				rubbish[i].nearRubbish++;
			}
			//用score记录四角垃圾数,记录得分 
			if(rubbish[i].x == rubbish[j].x+1&&rubbish[i].y==rubbish[j].y+1){
				rubbish[i].score++;
			}
			else if(rubbish[i].x == rubbish[j].x-1&&rubbish[i].y==rubbish[j].y-1){
				rubbish[i].score++;
			}
			else if(rubbish[i].x == rubbish[j].x-1&&rubbish[i].y==rubbish[j].y+1){
				rubbish[i].score++;
			}
			else if(rubbish[i].x == rubbish[j].x+1&&rubbish[i].y==rubbish[j].y-1){
				rubbish[i].score++;
			}
			
		}
	}
		for(int i = 0; i < n; i++){
			if(rubbish[i].nearRubbish == 4){
				//值为4符合回收站条件,接下来记录不同得分的回收站数目 
				scoreKind[rubbish[i].score]++;
			}
		}
		for(int i = 0; i < 5; i++){
			cout<<scoreKind[i]<<endl;
		}

} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值