牛牛在二维坐标系中画了N个点,且都是整点。现在牛牛想画出一个矩形,使得这N个点都在矩形内或者在矩形上。
矩形的边均平行于坐标轴。牛牛希望矩形的面积最小。请你帮助牛牛计算下最小矩形的面积。
首先输入一个正整数N表示点的个数(2 <= N <= 50)
接下来N行每行两个整数x, y,表示该点的坐标。绝对值均小于等于100.
一个整数表示最小矩形的面积。
2 0 1 1 0
1
#include<iostream> using namespace std; int findmin(int x[],int n) { int ret = 100; for(int i=0;i<n;i++) if(ret > x[i]) ret = x[i]; return ret; } int findmax(int x[],int n) { int ret = -100; for(int i=0;i<n;i++) if(ret < x[i]) ret = x[i]; return ret; } int main() { int n; cin >> n; int x[n],y[n]; for(int i=0;i<n;i++) { cin >> x[i] >> y[i]; } int x1,x2,y1,y2; x1 = findmin(x,n); x2 = findmax(x,n); y1 = findmin(y,n); y2 = findmax(y,n); int ret = (x2-x1)*(y2-y1); cout <<ret; return 0; }
#include <iostream> using namespace std; int main(){ int N,x,y,T,B,L,R,i; cin>>N; for(i=0;i<N;++i){ cin>>x>>y; L=(i==0||x<L)?x:L; R=(i==0||x>R)?x:R; B=(i==0||y<B)?y:B; T=(i==0||y>T)?y:T; } cout<<(T-B)*(R-L)<<endl; }
import java.util.Scanner; public class Main{ static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); int maxX = Integer.MIN_VALUE,minX = Integer.MAX_VALUE; int maxY = Integer.MIN_VALUE,minY = Integer.MAX_VALUE; for(int i=1;i<=2*n;i++){ int nums = in.nextInt(); if(i%2 ==0){ if(maxY < nums) maxY = nums; if(minY > nums) minY = nums; }else{ if(maxX < nums) maxX = nums; if(minX > nums) minX = nums; } } int y = maxY - minY; int x = maxX - minX; System.out.println(y*x); } }
# -*- coding: utf-8 -*- N = int(raw_input()) list_x_y = [] for i in xrange(0, N): list_x_y.append(map(int, raw_input().split())) x = zip(*list_x_y)[0] y = zip(*list_x_y)[1] x_min = min(x) x_max = max(x) y_min = min(y) y_max = max(y) s = (x_max - x_min)*(y_max - y_min) print s
#include<iostream> #include<limits> using namespace std; int main() { int n; cin >> n; int x,y; int min_x = INT16_MAX; int min_y = INT16_MAX; int max_x = INT16_MIN; int max_y = INT16_MIN; while(n--) { cin >> x >> y; min_x = min(min_x,x); max_x = max(max_x,x); min_y = min(min_y,y); max_y = max(max_y,y); } cout << (max_x-min_x)*(max_y-min_y); }
public static int juxing(int[] xarrys, int[] yarrys) { int left = xarrys[0]; int right = xarrys[0]; int top = yarrys[0]; int bottom = yarrys[0]; for (int i = 1; i < xarrys.length; i++) { int x = xarrys[i]; int y = yarrys[i]; if (x < left) { left = x; } if (x > right) { right = x; } if (y > top) { top = y; } if (y < bottom) { bottom = y; } } return (right - left) * (top - bottom); }
#include<iostream>#include<vector>#include<cmath>#include<algorithm>using namespace std;struct point {intxpos;intypos;};typedef struct point POINT;bool istrue(POINT point1, POINT point2) {returnpoint1.xpos>point2.xpos;}bool istrue1(POINT point1, POINT point2) {returnpoint1.ypos>point2.ypos;}voidwork() {intnum;vector<POINT> vec;cin >> num;for(inti = 0; i<num; ++i) {POINT _point;intx, y;cin >> x >> y;_point.xpos = x;_point.ypos = y;vec.push_back(_point);}sort(vec.begin(), vec.end(), istrue);POINT xmax = *(vec.begin());POINT xmin = *(vec.end() - 1);sort(vec.begin(), vec.end(), istrue1);POINT ymax = *(vec.begin());POINT ymin = *(vec.end() - 1);cout << abs(xmax.xpos - xmin.xpos)*abs(ymax.ypos - ymin.ypos) << endl;}intmain() {work();return0;}
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
int N;
cin>>N;
int x,y;
int X[N],Y[N];
for(int i=0;i<N;i++){ cin>>X[i]>>Y[i];
}
int minX=X[0],maxX=X[0],minY=Y[0],maxY=Y[0];
for(int i=0;i<N;i++){
if(X[i]<minX){
minX=X[i];
}
if(X[i]>maxX){
maxX=X[i];
}
if(Y[i]<minY){
minY=Y[i];
}
if(Y[i]>maxY){
maxY=Y[i];
}
}
int area = (maxX-minX)*(maxY-minY);
cout<<area;
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[]) {
int n;
cin>>n;
int p[n][2];
for (int i = 0; i < n; i++) {
cin>>p[i][0]>>p[i][1];
}
int x[n], y[n];
for (int i = 0; i < n; i++) {
x[i] = p[i][0];
y[i] = p[i][1];
}
sort(x, x + n);
sort(y, y + n);
cout<<(x[n - 1] - x[0]) * (y[n - 1] - y[0]);
return 0;
}
#include <iostream> #include <cstdlib> #include <math.h> using namespace std; void minOrMax(int a[],int &min,int &max,int length){ max=min=a[0]; for(int i=0;i<length;i++){ if(a[i]<min){ min = a[i]; } if(a[i]>max){ max = a[i]; } } } int main(int argc, char** argv) { int n; cin>>n; int x[n]; int y[n]; for(int i=0;i<n;i++){ cin>>x[i]>>y[i]; } int xMin,xMax,yMin,yMax; minOrMax(x,xMin,xMax,n); minOrMax(y,yMin,yMax,n); int result = abs(xMin-xMax) * abs(yMin-yMax); cout<<result; return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cmath> int main(){ using namespace std; int n; while(cin >> n){ vector<int> l(n); vector<int> w(n); for(int i = 0; i < n; i ++) cin >> l[i] >> w[i]; sort(l.begin(), l.end()); sort(w.begin(), w.end()); int x = l[l.size() - 1] - l[0]; int y = w[w.size() - 1] - w[0]; cout << x * y << endl; } return 0; }