回收站选址
定义结构体存放坐标和判断是否为垃圾站的变量和得分变量,输入所有坐标,刷新结构体内变量的值,最后通过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;
}
}