题目:

度度熊有一张网格纸,但是纸上有一些点过的点,每个点都在网格点上,若把网格看成一个坐标轴平行于网格线的坐标系的话,每个点可以用一对整数x,y来表示。度度熊必须沿着网格线画一个正方形,使所有点在正方形的内部或者边界。然后把这个正方形剪下来。问剪掉正方形的最小面积是多少。

输入描述:

第一行一个数n(2≤n≤1000)表示点数,接下来每行一对整数xi,yi(-1e9<=xi,yi<=1e9)表示网格上的点

输出描述:

一行输出最小面积

输入例子:

2
0 0
0 3

输出例子:

9

分析:

分别求得x坐标和y坐标的最大差值,再求二者的最大值。

算法代码:

int cutGridPaper(int n, int X[], int Y[])
{
    int minimunArea = 0;
    
    int minXLength, maxXLength, minYLength, maxYLength, XLength, YLength;
    
    minXLength = maxXLength = X[0];
    minYLength = maxYLength = Y[0];
    
    for (int i = 0; i < n; ++i) {
        if (X[i] > maxXLength) {
            maxXLength = X[i];
        }
        if (X[i] < minXLength) {
            minXLength = X[i];
        }
        if (Y[i] > maxYLength) {
            maxYLength = Y[i];
        }
        if (Y[i] < minYLength) {
            minYLength = Y[i];
        }
    }
    
    XLength = abs(maxXLength - minXLength);
    YLength = abs(maxYLength - minYLength);
    
    if (XLength > YLength) {
        minimunArea = XLength * XLength;
    }
    else
    {
        minimunArea = YLength * YLength;
    }
    
    return minimunArea;
}

测试代码:

//
//  main.cpp
//  cutGridPaper
//
//  Created by Jiajie Zhuo on 2017/4/23.
//  Copyright © 2017年 Jiajie Zhuo. All rights reserved.
//

#include <iostream>
#include <cmath>

using namespace std;

int cutGridPaper(int n, int X[], int Y[]);

int main(int argc, const char * argv[]) {
    int n;
    cout << "Please enter the number of point n: ";
    cin >> n;

    int X[n];
    int Y[n];
    cout << "Please enter the coordinates of points: ";
    for (int i = 0; i < n; ++i) {
        cin >> X[i] >> Y[i];
    }
    
    cout << "The minimun area of the square is " << cutGridPaper(n, X, Y) << endl;
    
    return 0;
}