题目:
解:
#include <iostream> using namespace std; class Point { public: Point(int x = 0, int y = 0) :x(x), y(y) {}; bool judge(Point& p2);//判断是否是符合条件的点 bool findPoint(Point& p2);//判断是否含有四角的点 int getX() { return x; } int getY() { return y; } void setX(int x) { this->x = x; } void setY(int y) { this->y = y; } static void finishPrint();//最终打印 void setPoint(int num);//根据得分增加相应得分的个数 private: int x, y; int pointN = 0; static int point0, point1, point2, point3, point4; }; int Point::point0 = 0; int Point::point1 = 0; int Point::point2 = 0; int Point::point3 = 0; int Point::point4 = 0; bool Point::judge(Point& p2) { if (x == p2.getX() && (y == p2.getY() + 1 || y == p2.getY() - 1)) { return true; } else if (y == p2.getY() && (x == p2.getX() + 1 || x == p2.getX() - 1)) { return true; } else return false; } bool Point::findPoint(Point& p2) { if ((p2.getX() == x + 1 || p2.getX() == x - 1) && (p2.getY() == y + 1 || p2.getY() == y - 1)) return true; else return false; } void Point::setPoint(int num) { switch (num) { case 0: point0++; break; case 1: point1++; break; case 2: point2++; break; case 3: point3++; break; case 4: point4++; break; default: break; } } void Point::finishPrint() { cout << point0 << endl << point1 << endl << point2 << endl << point3 << endl << point4 << endl; } int main() { int num; cin >> num; Point* points = new Point[num]; int x, y, flag; for (int i = 0; i < num; i++) { cin >> x >> y; points[i].setX(x); points[i].setY(y); } for (int i = 0; i < num; i++) { flag = 0; for (int j = 0; j < num; j++) { if (points[i].judge(points[j])) { flag++; } } if (flag == 4) { int pCount = 0; for (int k = 0; k < num; k++) { if (points[i].findPoint(points[k])) { pCount++; } } points[i].setPoint(pCount); } } Point::finishPrint(); delete(&points); return 0; }