연관문제: Four Boxes
각 변이 x, y축과 평행한 직사각형들이 주어질 때, 좌표평면에서 이 직사각형들이 차지하는 넓이를 구하시오.
(겹치는 부분은 한 번만 계산할 것)
입력으로 사각형 하나당 한 줄씩, 왼쪽 아래 꼭지점(x1, y1)과 오른쪽 위 꼭지점(x2, y2) 좌표가 주어진다.
단, 모든 좌표는 0 이상의 실수이다.
출력은 소수점 둘째 자리까지 출력한다.
입력:
0.0 0.0 4.0 4.0
1.0 1.0 3.0 3.0
출력:
16.00
입력:
1.0 0.0 5.0 2.0
2.0 0.0 4.0 5.0
0.0 1.0 6.0 3.0
10.0 10.0 12.0 12.0
출력:
24.00
21개의 풀이가 있습니다.
import re
# resolution = 0.01
RESOLUTION = 100
class Square:
def __init__(self, x1, y1, x2, y2):
self.x1 = min(x1, x2)
self.y1 = min(y1, y2)
self.x2 = max(x1, x2)
self.y2 = max(y1, y2)
def getPixels(self):
pix = []
xs = list(range(int(self.x1 * RESOLUTION), int(self.x2 * RESOLUTION)))
ys = list(range(int(self.y1 * RESOLUTION), int(self.y2 * RESOLUTION)))
for x in xs:
for y in ys:
pix.append((x, y))
return pix
def readSquares():
print('input squares (x1, y1, x2, y2)\n')
sqs = []
while 1:
inp = input()
if not re.match(r'^[0-9.]+ [0-9.]+ [0-9.]+ [0-9.]+$', inp):
break
x1, y1, x2, y2 = inp.split()
sq = Square(float(x1), float(y1), float(x2), float(y2))
sqs.append(sq)
return sqs
sqs = readSquares()
unions = set([])
for sq in sqs:
unions = unions | set(sq.getPixels())
print(len(unions)/RESOLUTION/RESOLUTION)
import numpy as np
point=[]
while True:
temp=input("Input two points:")
if not temp:
break
else: point.extend(list(map(float,temp.split())))
num=len(point)//4
point=np.array(point).reshape(num,4)*100
point=point.astype(int)
field=np.zeros((int(max(point[:,2])),int(max(point[:,3]))))
for i in range(num):
field[point[i,0]:point[i,2],point[i,1]:point[i,3]]=1
print("면적:%0.2f" %(field.sum()/10000))
기본적인 틀은 1번 문제와 같고, 1번 문제의 아이디어를 사용하면서 실수에 적용하기 위해 field 크기를 가로,세로 각각 100배로 늘려줍니다. 마지막에 면적을 얻기 위해 다시 10000을 나눕니다, 간단하지만 단점은 입력 값에 소수점 아래 세자리 수가 들어가기 시작하면 정확도가 떨어집니다.
public class Calculate {
public static void main(String[] args) {
//String [] s = {"0.0 0.0 4.0 4.0", "1.0 1.0 3.0 3.0"};
String [] s = {"1.0 0.0 5.0 2.0", "2.0 0.0 4.0 5.0", "0.0 1.0 6.0 3.0", "10.0 10.0 12.0 12.0"};
Float [] xPositions = new Float[2*s.length];
Float [] yPositions = new Float[2*s.length];
Float tmp = 0.0f;
Float result = 0.0f;
//입력된 위치정보를 x, y 배열로 각각 등록
for(int i=0 ; i<s.length ; i++) {
xPositions[2*i] = Float.parseFloat(s[i].split(" ")[0]);
yPositions[2*i] = Float.parseFloat(s[i].split(" ")[1]);
xPositions[2*i+1] = Float.parseFloat(s[i].split(" ")[2]);
yPositions[2*i+1] = Float.parseFloat(s[i].split(" ")[3]);
}
//나중 넓이 계산을 위해 x, y 좌표를 복사
Float [] xPositionsInput = xPositions.clone();
Float [] yPositionsInput = yPositions.clone();
//x배열을 오름차순으로 정렬
for(int i=0 ; i<xPositions.length ; i++){
for( int j=i ; j<xPositions.length ; j++){
if(xPositions[i] > xPositions[j]){
tmp = xPositions[i];
xPositions[i] = xPositions[j];
xPositions[j] = tmp;
}
}
}
//y배열을 오름차순으로 정렬
for(int i=0 ; i<yPositions.length ; i++){
for( int j=i ; j<yPositions.length ; j++){
if(yPositions[i] > yPositions[j]){
tmp = yPositions[i];
yPositions[i] = yPositions[j];
yPositions[j] = tmp;
}
}
}
// 정렬된 x, y 배열은 입력된 사각형들을 겹치는 위치를 기준으로 모두 쪼개서 각각의 위치정보를 저장하고 있음
// 이 각각의 조각들이 입력된 사각형들중 하나에라도 포함된다면 넓이에 합하면 됨
// 위에서 복사해둔 PositionsInput 배열로 입력된 사각형 위치정보 확인
for(int x=0 ; x<xPositions.length-1 ; x++){
for(int y=0 ; y<yPositions.length-1 ; y++){
for(int cnt=0 ; cnt<s.length ; cnt++){
//System.out.println("위치 = x : "+x+" y : "+y);
//System.out.println("시작점 = "+ xPositions[x]+", "+yPositions[y]);
//System.out.println("끝점 = "+ xPositions[x+1]+", "+yPositions[y+1]);
if( xPositions[x] >= xPositionsInput[2*cnt] && xPositions[x+1] <= xPositionsInput[2*cnt+1] &&
yPositions[y] >= yPositionsInput[2*cnt] && yPositions[y+1] <= yPositionsInput[2*cnt+1]) {
result += (xPositions[x+1] - xPositions[x]) * (yPositions[y+1] - yPositions[y]);
break;
}
}
}
}
System.out.println(result);
}
}
package 생성자;
import java.util.Scanner;
import java.util.jar.Attributes.Name;
/*
* 클래스 멤버 실행 초기화 순위가 1순위이다.
* main메서드 보다도 먼저
* 1)클래스 멤버를 초기화를 하여 올려 놓은다.
* 2)클래스 초기화 블럭으로 가서 수행문을 한다.
* 3)그 다음에는 main메서드가 호출된다.
*
* ===========================
* 인스턴스 생성시
*1)클래스 멤버과 위와 같이 작업이 된다. (3번을 빼고)
* 2)인스턴스 멤버과 초기화 되어 메모리 위에 올라간다.
* 3)참조변수에 주소가 저장된다.
*/
class Zombie{
static int Hp;
static {
Hp = 1000;
}
//static
String User;
{
User = "Anonymous";
}
public Zombie(String name) {
User = name;
}
public Zombie(int damage) {
Hp-=damage;
System.out.println("Name : "+User+"\nHp :"+Hp);
}
}
class Attack extends Zombie{
public Attack(int damage) {
super(damage);
Hp -= damage;
}
}
public class Damage {
public static void main(String[] args) {
System.out.println("Input you character name");
Zombie zombie = new Zombie(new Scanner(System.in).nextLine());
System.out.println("Input Damage you want");
Attack attack = new Attack(new Scanner(System.in).nextInt());
}
}
list_xy = []
for i in range (1, 5): list_comp = [] x = input("Point %s's X: " % i) list_comp.append(float(x)) y = input("Point %s's Y: " % i) list_comp.append(float(y)) list_xy.append(list_comp)
print (list_xy)
squr_k = float(0)
for i in range(3): squr_k = squr_k + (list_xy[i][0] * list_xy[i+1][1]) squr_k = squr_k - (list_xy[i+1][0] * list_xy[i][1])
squr_K = squr_k + (list_xy[3][0] * list_xy[0][1]) - (list_xy[0][0] * list_xy[3][1]) squr_k = abs(squr_k) * 1/2
print (round(squr_k, 2))
# 한글 처리 in Atom 1.21.1 + Anaconda(Python 3.6.3)
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8')
# 각 변이 x, y축과 평행한 직사각형들이 주어질 때, 좌표평면에서 이 직사각형들이 차지하는 넓이를 구하시오.
# (겹치는 부분은 한 번만 계산할 것)
# 입력으로 사각형 하나당 한 줄씩, 왼쪽 아래 꼭지점(x1, y1)과 오른쪽 위 꼭지점(x2, y2) 좌표가 주어진다.
# 단, 모든 좌표는 0 이상의 실수이다.
# 출력은 소수점 둘째 자리까지 출력한다.
squares = []
while True:
square = input("직사각형 (x1,y1)~(x2,y2)를 입력하세요(입력이 끝났으면 end) : ")
if square == "end":
break
square_point = square.split()
squares += [[float(square_point[i]) for i in range(4)]]
x = set([])
y = set([])
for i in range(len(squares)):
x.add(squares[i][0])
x.add(squares[i][2])
y.add(squares[i][1])
y.add(squares[i][3])
x = sorted(list(x))
y = sorted(list(y))
size = 0
for x_point in range(len(x) - 1):
for y_point in range(len(y) - 1):
for x1, y1, x2, y2 in squares:
if x[x_point] >= min(x1, x2) and x[x_point + 1] <= max(x1, x2) and y[y_point] >= min(y1, y2) and y[y_point + 1] <= max(y1, y2):
size += (x[x_point + 1] - x[x_point]) * (y[y_point + 1] - y[y_point])
break
print("총 면적 : %0.2f" % size)
입력에서 생기는 에러에 대항 처리는 안했습니다. 저한텐 문제풀이보다 그게 더 어렵네요;
q = 0
n = int(input("총 사각형의 갯수는 몇 개인가요?"))
data = []
while q < n :
s = str(input("좌표를 입력하세요. x1 y1 x2 y2 : "))
s = s.split(' ')
x1 = int(s[0])
y1 = int(s[1])
x2 = int(s[2])
y2 = int(s[3])
for i in range (x1, x2) :
for j in range (y1, y2) :
x = (i+1, j+1)
data.append(x)
# print(data)
q += 1
data2 = set(data)
area = len(data2)
print(area)
#include <stdio.h>
#include <math.h>
#include <memory.h>
struct SquareInfo
{
float x1, y1;
float x2, y2;
float GetWidth() { return x2 - x1; }
float GetHeight() { return y2 - y1; }
float GetArea() { return GetWidth() * GetHeight(); }
bool CheckSquareInside(SquareInfo& CheckSquare)
{
return (x1 <= CheckSquare.x1) && (y1 <= CheckSquare.y1)
&& (CheckSquare.x2 <= x2) && (CheckSquare.y2 <= y2);
}
};
#define EMPTY_SPACE 0xFFFFFFFF
class CDojo150_SquareArea
{
public:
CDojo150_SquareArea() {};
~CDojo150_SquareArea() {};
float Solve(const float* inputVal, const int numOfSquare);
private:
bool CheckExistNum(float* ScaleList, int ScalListNum, float Val);
void SortScaleList(float* ScaleList, int ScaleListSize);
};
float CDojo150_SquareArea::Solve(const float* inputVal, const int numOfSquare)
{
float retVal = 0.0f;
float* xScaleList = new float[numOfSquare * 2];
memset(xScaleList, EMPTY_SPACE, sizeof(float) * (numOfSquare * 2));
float* yScaleList = new float[numOfSquare * 2];
memset(yScaleList, EMPTY_SPACE, sizeof(float) * (numOfSquare * 2));
int xScaleListNum = 0;
int yScaleListNum = 0;
SquareInfo* SquareList = new SquareInfo[numOfSquare];
for (int i = 0; i < numOfSquare; i++)
{
SquareList[i].x1 = inputVal[4 * i];
SquareList[i].y1 = inputVal[(4 * i + 1)];
SquareList[i].x2 = inputVal[(4 * i + 2)];
SquareList[i].y2 = inputVal[(4 * i + 3)];
if (CheckExistNum(xScaleList, xScaleListNum, SquareList[i].x1) == false)
xScaleList[xScaleListNum++] = SquareList[i].x1;
if (CheckExistNum(xScaleList, xScaleListNum, SquareList[i].x2) == false)
xScaleList[xScaleListNum++] = SquareList[i].x2;
if (CheckExistNum(yScaleList, yScaleListNum, SquareList[i].y1) == false)
yScaleList[yScaleListNum++] = SquareList[i].y1;
if (CheckExistNum(yScaleList, yScaleListNum, SquareList[i].y2) == false)
yScaleList[yScaleListNum++] = SquareList[i].y2;
}
SortScaleList(xScaleList, xScaleListNum);
SortScaleList(yScaleList, yScaleListNum);
for (int yScaleIdx = 0; yScaleIdx < yScaleListNum - 1; yScaleIdx++)
{
for (int xScaleIdx = 0; xScaleIdx < xScaleListNum - 1; xScaleIdx++)
{
SquareInfo checkSquare;
checkSquare.x1 = xScaleList[xScaleIdx];
checkSquare.y1 = yScaleList[yScaleIdx];
checkSquare.x2 = xScaleList[xScaleIdx + 1];
checkSquare.y2 = yScaleList[yScaleIdx + 1];
for (int squareIdx = 0; squareIdx < numOfSquare; squareIdx++)
{
if (SquareList[squareIdx].CheckSquareInside(checkSquare))
{
retVal += checkSquare.GetArea();
break;
}
}
}
}
delete[] xScaleList;
delete[] yScaleList;
delete[] SquareList;
return retVal;
}
bool CDojo150_SquareArea::CheckExistNum(float* ScaleList, int ScalListNum, float Val)
{
for (int i = 0; i < ScalListNum; i++)
{
if (ScaleList[i] != EMPTY_SPACE)
continue;
if (ScaleList[i] == Val)
return true;
}
return false;
}
void CDojo150_SquareArea::SortScaleList(float* ScaleList, int ScaleListSize)
{
for (int i = 0; i < ScaleListSize; i++)
{
for (int j = i; j < ScaleListSize; j++)
{
if (ScaleList[j] < ScaleList[i])
{
float temp = ScaleList[i];
ScaleList[i] = ScaleList[j];
ScaleList[j] = temp;
}
}
}
}
int main()
{
CDojo150_SquareArea exam;
float SquareInfo[] = {
1.0, 0.0, 5.0, 2.0,
2.0, 0.0, 4.0, 5.0,
0.0, 1.0, 6.0, 3.0,
10.0, 10.0, 12.0, 12.0
};
printf("%.2f\n", exam.Solve(SquareInfo, 4));
}
파이썬 3.6
data ="""1.0 0.0 5.0 2.0
2.0 0.0 4.0 5.0
0.0 1.0 6.0 3.0
10.0 10.0 12.0 12.0"""
def squarearea(square):
area = (float(square[2]) - float(square[0])) * (float(square[3]) - float(square[1]))
return area
def main(squarelist):
arealist,shade,shadelist,shadearea,n = [],[],[],[],0
for i, square in enumerate(squarelist):
arealist.append(squarearea(square))
# 겹치는 부분을 찾아 면적을 구한다.
# 자신 이외의 모든 사각형에 대해 겹치는 부분을 찾는다.
if len(arealist) > 1:
while n < i:
if (float(squarelist[n][2]) <= float(square[0]) and float(squarelist[n][3]) <= float(square[1])) or (float(squarelist[n][0]) >= float(square[2]) and float(squarelist[n][1]) >= float(square[3])):
n += 1
else:
shade = [max(squarelist[n][0],square[0]),max(squarelist[n][1],square[1]),min(squarelist[n][2],square[2]),min(squarelist[n][3],square[3])]
shadearea.append(squarearea(shade))
shadelist.append(shade)
n += 1
n = 0
# 겹치는 부분이 2개 이상일때, 중복되어 겹치는 부분을 찾는다.
# 이부분은 겹치는 부분 면적에서 제외한다.
if len(shadelist) > 1:
while n < len(shadelist)-1:
if (float(shadelist[n][2]) <= float(shadelist[len(shadelist)-1][0]) and float(shadelist[n][3]) <= float(shadelist[len(shadelist)-1][1])) or (float(shadelist[n][0]) >= float(shadelist[len(shadelist)-1][2]) and float(shadelist[n][1]) >= float(shadelist[len(shadelist)-1][3])):
n += 1
else:
shade = [max(shadelist[n][0],shadelist[len(shadelist)-1][0]),max(shadelist[n][1],shadelist[len(shadelist)-1][1]),min(shadelist[n][2],shadelist[len(shadelist)-1][2]),min(shadelist[n][3],shadelist[len(shadelist)-1][3])]
shadearea.append(float('-'+str(squarearea(shade))))
n += 1
n = 0
shadelist = []
print("☞직사각형들의 면적 = %.2f "%(sum(arealist)-sum(shadearea)))
if __name__ == "__main__":
squarelist = [i.split(' ') for i in data.split("\n")]
print("▶직사각형 좌표")
for square in squarelist:
print("(%s,%s) (%s,%s)"%(square[0],square[1],square[2],square[3]))
print("\n")
main(squarelist)
▶직사각형 좌표
(0.0,0.0) (4.0,4.0)
(1.0,1.0) (3.0,3.0)
☞직사각형들의 면적 = 16.00
▶직사각형 좌표
(1.0,0.0) (5.0,2.0)
(2.0,0.0) (4.0,5.0)
(0.0,1.0) (6.0,3.0)
(10.0,10.0) (12.0,12.0)
☞직사각형들의 면적 = 24.00
def area(a):
result = 0
xcoordinate = list()
ycoordinate = list()
for i in a:
xcoordinate.append(i[0])
xcoordinate.append(i[2])
ycoordinate.append(i[1])
ycoordinate.append(i[3])
xcoordinate = sorted(list(set(xcoordinate)))
ycoordinate = sorted(list(set(ycoordinate)))
for i in range(len(xcoordinate)-1):
for j in range(len(ycoordinate)-1):
if any([all([k[0] <= xcoordinate[i], k[1] <= ycoordinate[j], k[2] >= xcoordinate[i+1], k[3] >= ycoordinate[j+1]]) for k in a]):
result += (xcoordinate[i+1]-xcoordinate[i]) * (ycoordinate[j+1]-ycoordinate[j])
return "{0:0.2f}".format(result)
l = list()
while 1:
n = input("좌표를 입력하시오 :")
if n == '':
break
else:
l.append(list(map(float, n.split(' '))))
print(area(l))
n = int(input("꼭짓점 개수: "))
a = [input().split() for i in range(n)]
for i in range(n):
a[i] = list(map(float,a[i]))
s = 0
for i in range(n):
x_min = int(min([a[i][0] for i in range(n)]))
x_max = int(max([a[i][2] for i in range(n)]))
y_min = int(min([a[i][1] for i in range(n)]))
y_max = int(max([a[i][3] for i in range(n)]))
for x in range(x_min, x_max):
for y in range(y_min, y_max):
k = 0
for i in range(n):
if a[i][0] <= x <a[i][2] and a[i][1] <= y <a[i][3]:
k = 1
if k == 1:
s += 1
print(s)
비트 연산으로 풀어 봤습니다 넓이는 소수점 2자리까지 구합니다
from functools import reduce
coord = []
while 1:
coord.append( tuple(float(t) for t in input('좌표 입력(입력 종료는 엔터): ').split()) )
if coord[-1] == ():
del coord[-1]
break
x1,x2,y1,y2 = [],[],[],[]
for i in coord:
x1 += [int(i[0]*10)]
y1 += [int(i[1]*10)]
x2 += [int(i[2]*10)]
y2 += [int(i[3]*10)]
grid = [['' for _ in range(max(y2))] for _ in range(len(coord))]
for i in range(len(coord)):
for y in range(max(y2)):
for x in range(max(x2)):
if x1[i] <= x < x2[i] and y1[i] <= y < y2[i]:
grid[i][y] += '1'
else: grid[i][y] += '0'
result = reduce(lambda t1,t2: [str(bin(int(t1[i],2) | int(t2[i],2))[2:]).zfill(len(t1[0])) for i in range(len(t1))],grid)
for i in reversed(range(len(result))):
print(result[i].replace('0','○ ').replace('1','● '))
print('\n넓이: {:0.2f}'.format(sum(sum(int(j) for j in i) for i in result)/100))
좌표 입력(입력 종료는 엔터): 1.0 0.0 5.0 2.0
좌표 입력(입력 종료는 엔터): 2 0 4 5.0
좌표 입력(입력 종료는 엔터): 0 1.0 6 3
좌표 입력(입력 종료는 엔터): 10.0 10 12 12.0
좌표 입력(입력 종료는 엔터):
...
넓이: 24.00
class Square:
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
def pixel(self):
pix = []
xscale = list(range(int(self.x1 * 100), int(self.x2 * 100)))
yscale = list(range(int(self.y1 * 100), int(self.y2 * 100)))
for i in xscale:
for j in yscale:
pix.append((i,j))
return pix
def make_squares():
squares = []
while 1:
x1, y1, x2, y2 = input().split()
if x1 == '0' and y1 == '0' and x2 == '0' and y2 == '0':
break
square = Square(float(x1), float(y1), float(x2), float(y2))
squares.append(square)
return squares
squares = make_squares()
unions = set([])
for square in squares:
unions = set.union(unions, set(square.pixel()))
print(len(unions)/10000)
Python 3.7
- 집합 자료형-합집합을 이용하여 도형의 겹침을 해결했습니다.
- 10**소수점 자리수(prec) 만큼 좌표를 배율하여 접근했습니다.
- 코드에서 정확도 변수 prec를 0으로 두면, 문제에서 언급하신 Four Boxes 문제에도 적용 가능합니다.
prec = 2 # 숫점 이하 자리수(정확도)
def update_grid(posx1, posy1, posx2, posy2):
new_grid = set()
for y in range(int(posy1 * 10 ** prec), int(posy2 * 10 ** prec)):
for x in range(int(posx1 * 10 ** prec), int(posx2 * 10 ** prec)):
new_grid.add((x, y))
return new_grid
def print_area(istr):
grid = set()
lines = istr.split('\n')
for line in lines:
grid |= update_grid(*map(float, line.split())) # 좌표의 합집합
print(f"{len(grid) / (10 ** (2 * prec)):.2f}")
# --- test cases ---
test_case = """0.0 0.0 4.0 4.0
1.0 1.0 3.0 3.0"""
print_area(test_case) # 16.00
test_case = """1.0 0.0 5.0 2.0
2.0 0.0 4.0 5.0
0.0 1.0 6.0 3.0
10.0 10.0 12.0 12.0"""
print_area(test_case) # 24.00
test_case = """1 2 4 4
2 3 5 7
3 1 6 5
7 3 8 6""" # Four Boxes 문제
print_area(test_case) # 26.00
무식하게 풀었습니다...가독성이 떨어집니다.. 저는 X좌표를 기준으로 첫번째 예제의 경우 X좌표가 (0~4)와 (1~3)이 있으면 더 넓은 범위의 (0~4)를 기준으로 1씩증가하면서 그에 해당하는 넓이를 구했습니다. X가 0~1일때 Y의 넓이는 4, X가 1~2일대 첫번째 사각형은 넓이가 4, 2번째 사각형은 넓이가 2입니다. 그래서 0~1의 범위의 Y값은 4+2-2(공통부분)이므로 4입니다. 마찬가지로 다른 범위도 이런식으로 해서 풀었습니다...
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many rectangle : ");
int N = scan.nextInt();
Scanner scan2 = new Scanner(System.in);
ArrayList<Double> X = new ArrayList<Double>();
ArrayList<Double> Y = new ArrayList<Double>();
ArrayList<Double> A = new ArrayList<Double>();
for(int i=0; i<N; i++) {
String[] str = scan2.nextLine().split(" ");
for(int j=0; j<4; j++) {
if(j==1||j==3) {
Y.add(Double.parseDouble(str[j]));
}
else{
X.add(Double.parseDouble(str[j]));
}
}
}
Double small = X.get(0);
Double large = 0.0;
for(int i=0; i<X.size(); i++) {
if(X.get(i)>large) {
large = X.get(i);
}
else if(X.get(i)<small) {
small = X.get(i);
}
}
Double 넓이 = 0.0;
for(Double i = small; i<large; i++) {
int count = 0;
for(int j=0; j<X.size(); j=j+2) {
if(X.get(j)<=i&&X.get(j+1)>=i+1) {
넓이=넓이 + (Y.get(j+1)-Y.get(j));
count++;
for(Double l=Y.get(j); l<=Y.get(j+1); l++) {
A.add(l);
}
}
}
if(count>1) {
int count2 = 0;
for(int s =0; s<A.size()-1; s++) {
for(int a=s+1; a<A.size(); a++) {
if(A.get(s).compareTo(A.get(a))==0) {
A.remove(a);
count2++;
}
}
}
넓이 = 넓이-(count2-(count-1));
}
A.clear();
}
System.out.println(넓이);
}
}
for _ in range(4):
x1, y1, x2, y2 = map(int, input('input: ').split())
for i in range(x1, x2):
for j in range(y1, y2):
demention[i][j] = 1
area = 0
for row in demention:
area += sum(row)
print('Area: {0:.2f}'.format(area))
def background(x, y):
x_max = x*100
y_max = y*100
column = []
for i in range(x_max):
row = []
for j in range(y_max):
row.append(0)
column.append(row)
return column
def extract(_list):
cnt = 0
for member in _list:
cnt += member.count(1)
return cnt
if __name__ == '__main__':
array = background(10, 10)
input_1 = input('INPUT NUMBER : ')
input_list = []
for i in range(int(input_1)):
input_list.append(input('TYPE COORDINATION : '))
for member in input_list:
x1 = int(float(member.split(' ')[0]))*10
y1 = int(float(member.split(' ')[1]))*10
x2 = int(float(member.split(' ')[2]))*10
y2 = int(float(member.split(' ')[3]))*10
for i in range(x1, x2):
for j in range(y1, y2):
if array[i][j] != 1:
array[i][j] = 1
print('AREA : {:0.2f}'.format(extract(array)/100))
#codingdojing_area_of_square_re
#input이 소수 둘째짜리까지 있다고 가정한다. 1.12 1.22 2.34 2.1
#풀이 아이디어 참고, 100배 늘려서 정수형태로 만든다.
square_list = list()
area_set = set()
resolution = 100
while True:
square_coordinate = list(map(lambda x:int(float(x)*resolution),input().split()))
if square_coordinate:
square_list.append(square_coordinate)
else:
break
for square in square_list: #square = [x1, y1, x2, y2]
for x in range(square[0], square[2]):
for y in range(square[1], square[3]):
area_set.add((x,y)) # 크기가 1인 단위사각형의 왼쪽아래 좌표를 저장. 중복 제거.
print(round(len(area_set)/resolution**2, 2))
// Rust
pub struct Rectangle {
pub xrange: (u32, u32),
pub yrange: (u32, u32),
} fn rectangle() -> u32 {
// let rectangles = vec![ Rectangle{ xrange: (0, 4), yrange: (0, 4)},
// Rectangle{ xrange: (1, 3), yrange: (1, 3)},];
let rectangles = vec![ Rectangle{ xrange: (1, 5), yrange: (0, 2)},
Rectangle{ xrange: (2, 4), yrange: (0, 5)},
Rectangle{ xrange: (0, 6), yrange: (1, 3)},
Rectangle{ xrange: (10, 12), yrange: (10, 12)}];
let mut xintervals = vec![];
for rect in &rectangles {
xintervals.push(rect.xrange);
}
xintervals = tidy_interval(xintervals);
let mut area = 0;
for xinterval in xintervals {
for x in xinterval.0..xinterval.1 {
let mut yinterval = vec![];
for rect in &rectangles {
if rect.xrange.0 <= x && x < rect.xrange.1 {
yinterval.push(rect.yrange);
}
}
area += len(tidy_interval(yinterval));
}
}
area
}
fn tidy_interval(intervals_: Vec<(u32, u32)>) -> Vec<(u32, u32)> {
let mut intervals = intervals_;
intervals.sort_by_key(|w| w.0);
let mut i = 1;
while i < intervals.len() {
if intervals[i].0 <= intervals[i-1].1 {
if intervals[i].1 >= intervals[i-1].1 {//앞으로merge, i그대로
intervals[i-1].1 = intervals[i].1;
intervals.remove(i);
continue;
}
else {// delete, i 그대로
intervals.remove(i);
continue;
}
} //else {} //continue, i증가
i += 1;
}
intervals
}
fn len(intervals: Vec<(u32, u32)>) -> u32 {
let mut len = 0;
for interval in &intervals {
len += interval.1 - interval.0;
}
len
}
a='''1.0 0.0 5.0 2.0
2.0 0.0 4.0 5.0
0.0 1.0 6.0 3.0
10.0 10.0 12.0 12.0'''
import random
x_min, x_max, y_min, y_max = 0,0,0,0
for i in range(len(a.split('\n'))):
x1, y1, x2, y2 = map(float,a.split('\n')[i].split(' '))
x_min, x_max = min(x_min, x1, x2), max(x_max, x1, x2)
y_min, y_max = min(y_min, y1, y2), max(y_max, y1, y2)
t = 1000000
p = 0
for i in range(t):
cnt = 0
x = random.uniform(x_min,x_max)
y = random.uniform(y_min,y_max)
for i in range(len(a.split('\n'))):
x1, y1, x2, y2 = map(float,a.split('\n')[i].split(' '))
if x1<= x <=x2 and y1<= y <=y2:
cnt += 1
if cnt > 0:
p += 1
print(round((p/t)*(x_max-x_min)*(y_max-y_min),2))
#coordinates = [[0.0, 0.0, 4.0, 4.0], [1.0, 1.0, 3.0, 3.0]]
coordinates = [[0.0, 1.0, 6.0, 3.0], [1.0, 0.0, 5.0, 2.0],
[2.0, 0.0, 4.0, 5.0], [10.0, 10.0, 12.0, 12.0]]
area = [[0 for _ in range(1000)] for _ in range(1000)]
cnt = 0
for p in coordinates:
for r in range(int(p[1]*10), int(p[3]*10)):
for c in range(int(p[0]*10), int(p[2]*10)):
if area[r][c] == 0:
area[r][c] = 1
cnt += 1
print(cnt / 100)