有一个城市需要修建,给你N个民居的坐标X,Y,问把这么多民居全都包进城市的话,城市所需最小面积是多少(注意,城市为平行于坐标轴的正方形)
#include<iostream> using namespace std; const long long LONG_MIN=(1<<63); const long long LONG_MAX=(1<<63)-1; int main() { long long num; long long x=0,y=0; long long minX=LONG_MAX,minY=LONG_MAX,maxX=LONG_MIN,maxY=LONG_MIN; cin>>num; for(long long i=0;i<num;i++) { cin>>x>>y; if(x<=minX) minX=x; if(x>=maxX) maxX=x; if(y<=minY) minY=y; if(y>=maxY) maxY=y; } long long xLength=abs(maxX-minX); long long yLength=abs(maxY-minY); long long edgeLength=xLength >= yLength ? xLength: yLength; cout<<edgeLength*edgeLength; return 0; }
//大数相乘啊!!!朋友们。。。。 var num = readline(); var house_x = [],house_y = [],tmp; while(tmp = readline()){ tmp = tmp.split(' '); house_x.push(tmp[0]); house_y.push(tmp[1]); } if(num == house_x.length && num == house_y.length){ getArea(house_x,house_y,num); } function getArea(house_x,house_y,num){ if(num<2) {console.log(0);} else{ house_x.sort((a,b)=>{return a-b}); house_y.sort((a,b)=>{return a-b}); var x = Math.abs(house_x[num-1]-house_x[0]); var y = Math.abs(house_y[num-1]-house_y[0]); var side = Math.max(x,y); //哦呵呵呵。。。。。居然还有大数相乘这种鬼,查了我一宿 var result = multi(side.toString(),side.toString()) console.log(result) } } function multi(a,b){ var str1,str2,len1,len2,maxlen,result = []; str1 = a.split("").reverse(); str2 = b.split("").reverse(); len1 = str1.length; len2 = str2.length; //因为要在下一步做累加,如果不初始化为0,result[]中的值会变为NaN //因为未初始化的数组中的值为undefined for(var i = 0;i < len1;i++) for(var j = 0;j < len2;j++) result[i + j] = 0; for(var i = 0;i < len1;i++) for(var j = 0;j < len2;j++) //根据乘法的手动计算方式,在上下相同位上会有相加 result[i + j] += parseInt(str1[i]) * parseInt(str2[j]); var n = result.length; for(var k = 0;k < n-1;k++) { var temp = result[k]; if(temp >= 10) { result[k] = temp % 10; //JS中的"/"不是除法取整,会取得小数,所以要用Math.floor() result[k + 1] += parseInt(temp / 10); } } return result.reverse().join(""); }
import java.util.Arrays; import java.util.Scanner; /** * @Author:likui * @PacakgeName:城市所需最小面积 * @Description: * @Date:Created in 20:37 2019/8/9 */ public class Main { public static long Way(long x[],long y[]){ Arrays.sort(x); Arrays.sort(y); if (x.length<2||y.length<2) return 0; long x_length=Math.abs(x[x.length-1]-x[0]); long y_length=Math.abs(y[y.length-1]-y[0]); return Math.max(x_length,y_length); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); long X[]=new long[n]; long Y[]=new long[n]; for (int i = 0; i <n ; i++) { X[i]=sc.nextLong(); Y[i]=sc.nextLong(); } long res=Way(X,Y); System.out.println(res*res); } }得到x轴最小和最大值的差,得到y轴最小和最大值的差,比较两者,得到最大的那个,用它平方即可
#include <iostream> using namespace std; long long max(int a, int b) { return a > b ? a : b; } long long min(int a, int b) { return a < b ? a : b; } int main() { long long n = 0; long long x, y; long long min_x = 0, min_y = 0, max_x = 0, max_y = 0; cin >> n; cin >> x; cin >> y; max_x = min_x = x; max_y = min_y = y; while (cin) { cin >> x; cin >> y; max_x = max(x, max_x); max_y = max(y, max_y); min_x = min(x, min_x); min_y = min(y, min_y); } long long l = (max_x - min_x); long long ll = (max_y - min_y); long long lll = max(l, ll); long long llll = lll * lll; cout << llll << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for (ll i=x;i<=y;i++) typedef long long ll; const int INF=2147000000; int main() { ios::sync_with_stdio(false); int n; cin>>n; int x,y,maxx=-1,maxy=-1,minx=INF,miny=INF; rep(i,1,n) { cin>>x>>y; maxx=max(maxx,x); minx=min(minx,x); maxy=max(maxy,y); miny=min(miny,y); } int ans; ans=max(maxx-minx,maxy-miny); cout<<ans*ans<<endl; return 0; }望采纳,谢谢~
public int MaxArea(){ // 获取个数 Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] Xs = new int[n]; int[] Ys = new int[n]; int MaxX = Integer.MIN_VALUE; int MinX = Integer.MAX_VALUE; int MaxY = Integer.MIN_VALUE; int MinY = Integer.MAX_VALUE; for(int i=0 ; i<n ; i++){ // 输入同时求得最值 int x = scanner.nextInt(); int y = scanner.nextInt(); if(x > MaxX){ MaxX = x; } if(x < MinX){ MinX = x; } if(y > MaxY){ y = MaxY; } if(y < MinY){ MinY = y; } } // 根据最值求面积 int len_x = MaxX - MinX; int len_y = MaxY - MinY; return len_x > len_y?(len_x * len_x):(len_y * len_y); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 其实就是x轴存储一个最大值,最小值,y轴存储一个最大值,最小值, 然后大的差值相乘即可. import java.util.Scanner; public class Main { public static void main(String[] args) { //1.接收输入 Scanner sc = new Scanner(System.in); int number = sc.nextInt(); //居民数 long[] rece = new long[number];//接收数组 //第一组数据既是最大值,又是最小值 long xMax=sc.nextLong(); long yMax=sc.nextLong(); long xMin=xMax; long yMin=yMax; long xhelp=0; long yhelp=0; for(int i = 1; i < rece.length; i++) { xhelp=sc.nextLong(); if(xhelp>xMax) xMax=xhelp; if(xhelp<xMin) xMin=xhelp; yhelp=sc.nextLong(); if(yhelp>yMax) yMax=yhelp; if(yhelp<yMin) yMin=yhelp; } long MaxSide=Math.abs(xMax-xMin)>Math.abs(yMax-yMin) ? Math.abs(xMax-xMin) : Math.abs(yMax-yMin); System.out.println(MaxSide*MaxSide); } } |
import java.util.*; public class Main{ public static void main(String[] args) { System.out.println(constructCity()); } public static long constructCity() { Scanner sc = new Scanner(System.in); int numHouse = sc.nextInt(); int count = numHouse; long plotX,plotY,diffX,diffY; long maxX=0, maxY=0 ,minX=0, minY=0; while (count-- > 0) { if ((count + 1) == numHouse) { maxX = minX = sc.nextLong(); maxY = minY = sc.nextLong(); continue; } plotX = sc.nextLong(); plotY = sc.nextLong(); if (plotX > maxX || plotX < minX) { if (plotX > maxX) { maxX = plotX; } if (plotX < minX) { minX = plotX; } } if (plotY > maxY || plotY < minY) { if (plotY > maxY) { maxY = plotY; } if (plotY < minY) { minY = plotY; } } } diffX = Math.abs(maxX - minX); diffY = Math.abs(maxY - minY); return diffX > diffY? diffX*diffX:diffY*diffY; } }
用例: 2 -545182011 148174425 -819653699 414310201 对应输出应该为:(这里的答案应该有误,准确答案应该是我的输出,说白了就是819653699的平方) 75334707513569344 你的输出为: 671832186284382601题目还是很简单的,注意一下大数的问题就行了
/* 规则说白了,就是寻找最大X的长度,以及Y长度,然后找出最大的值进行平方 */ public class 城市修建 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); //这里区分一下正负,因为如果一正一负的话,那距离是正轴加负轴 Long xPosMax = 0L; Long yPosMax = 0L; Long xNegMax = 0L; Long yNegMax = 0L; for (int i = 0; i < n; i++) { Long x = in.nextLong(); if (x > 0) xPosMax = xPosMax > x ? xPosMax : x; if (x < 0) xNegMax = xNegMax < x ? xNegMax : x; Long y = in.nextLong(); if (y > 0) yPosMax = yPosMax > y ? yPosMax : y; if (y < 0) yNegMax = yNegMax < y ? yNegMax : y; } Long x = xPosMax - xNegMax; Long y = yPosMax - yNegMax; //这里针对数据溢出问题直接使用了BigInteger,注意一下BigInteger没有减法,只有取反negate() BigInteger bigPosX = BigInteger.valueOf(xPosMax); BigInteger bigNegX = BigInteger.valueOf(xNegMax); BigInteger maxX = bigPosX.add(bigNegX.negate()); BigInteger bigPosY = BigInteger.valueOf(yPosMax); BigInteger bigNegY = BigInteger.valueOf(yNegMax); BigInteger maxY = bigPosY.add(bigNegY.negate()); if (x > y) System.out.println(maxX.pow(2)); else System.out.println(maxY.pow(2)); } }
while True: try: a = int(input()) b = [] for i in range(a): b.append(list(map(int, input().split()))) up = max(j[0] for j in b) down = min(j[0] for j in b) left = min(j[1] for j in b) right = max(j[1] for j in b) print(max(up - down, right - left) ** 2) except: break
n = int(input()) x = [] y = [] for i in range(n): a, b = map(int,input().split()) x.append(a) y.append(b) print(max(abs(max(x)-min(x)),abs(max(y)-min(y)))**2)
num = int(input()) xlst = [] ylst = [] for i in range(num): newpoint = input() xlst.append(int(newpoint.split()[0])) ylst.append(int(newpoint.split()[1])) xlen = max(xlst) - min(xlst) ylen = max(ylst) - min(ylst) area = max(xlen * xlen, ylen * ylen) print(area)
import java.util.Scanner; /** * @author :xbb * @date :Created in 2020/3/27 8:58 上午 * @description:牛客网刷题 * @modifiedBy: * @version: */ public class Main { public static void main(String[] args) { CityBuilding(); } /** * 城市修建 * 有一个城市需要修建,给你N个民居的坐标X,Y, * 问把这么多民居全都包进城市的话,城市所需最小面积是多少(注意,城市为平行于坐标轴的正方形) */ private static void CityBuilding() { Scanner in = new Scanner(System.in); int n = in.nextInt(); Long xMax = Long.MIN_VALUE; Long yMax = Long.MIN_VALUE; Long xMin = Long.MAX_VALUE; Long yMin = Long.MAX_VALUE; for (int i = 0; i < n; i++) { Long x = in.nextLong(); xMax = xMax > x ? xMax : x; xMin = xMin < x ? xMin : x; Long y = in.nextLong(); yMax = yMax > y ? yMax : y; yMin = yMin < y ? yMin : y; } Long x = xMax - xMin; Long y = yMax - yMin; if (x.compareTo(y) == 1) { System.out.println(x*x); } else { System.out.println(y*y); } } }