x,y 좌표들로 교차되는 부분을 계산해서 수식을 세울 수 있지 않을까? 잠깐 고민했다가 정수 범위가 1~100까지여서 이중 for문으로 구현한 문제
# 네 사각형의 합집합 면적 구하기
# 모든 x좌표와 y좌표는 1이상이고 100이하인 정수이다.
# 접근 방법 : 어차피 100까지 밖에 길이가 없으니 101*101 2d 행렬에 1의 값을 더하면 총 면적이 된다.
tot = 0
arr=[[0]*101 for _ in range(101)]
for _ in range(4):
x1,y1,x2,y2 = map(int,input().split())
for i in range(x1,x2):
for j in range(y1,y2):
if not arr[i][j]: # 중복을 피하기 위해 해당 위치에
arr[i][j]=1 # 1을 만들어줄 수 있을 때만 면적 +1
tot+=1
print(tot)
s={0} # 애초에 중복을 허용하지 않는 set(집합) 자료구조 활용
# 가시성을 위해 들여쓰기를 하여 구분하였다.
for i in open(0):
a,b,c,d=map(int,i.split())
# 내 풀이와 같이 이중 for문으로 (x,y)원소가 포함된 집합을
# `|`(합집합 = union) 연산을 통해 s에 포함되도록 하였다.
s|={(x,y)for x in range(a,c)for y in range(b,d)}
print(len(s)-1) # 마지막 - 1은 아마 원소 `0`이 포함되었기 때문에
# 이를 면적 계산에서 제거하기 위해 넣었다.
위의 python 숏코딩 로직을 c++로 구현할 경우 아래와 같다.
#include <iostream>
#include <set>
using namespace std;
int main(){
set<pair<int, int>> s;
int a,b,c,d;
while(cin >> a >> b >> c >> d){
for (int x=a;x<c;x++){
for(int y=b;y<d;y++){
s.emplace(x,y);
}
}
}
cout << s.size();
}
#import <ios> // include header file
int d[101][101],x,y,a,b,i,j,s; // declare variables and 2D array d
main() { // main function
while(~scanf("%d%d%d%d",&x,&y,&a,&b)) // read integers until EOF
for(i=x; i<a; i++) // loop through x to a-1
for(j=y; j<b; j++) // loop through y to b-1
d[i][j]++; // increment value of d[i][j]
for(i=0; i<101; i++) // loop through rows of d
for(j=0; j<101; j++) // loop through columns of d
d[i][j] ? s++ : s; // increment s if d[i][j] is non-zero, otherwise do nothing
printf("%d",s); // print value of s
}
In C++, the
scanf
function is used to read formatted input from standard input (stdin). The format specifier%d
is used to read an integer value.
The code
while (~scanf("%d%d%d%d",&x,&y,&a,&b))
reads four integer values from standard input repeatedly until the end of file (EOF
) marker is encountered. The~
operator is a bitwise NOT operator, which in this case, is used to invert the value returned by scanf.
The
scanf
function returns the number of input items successfully matched and assigned, which should be equal to the number of format specifiers in the format string. If scanf fails to read an input item, it returnsEOF
. The~
operator inverts the value ofEOF
to-1
, which is a non-zero value that can be used as a true condition in the while loop.
So the
while (~scanf("%d%d%d%d",&x,&y,&a,&b))
loop continues to read four integers at a time from standard input until scanf returns a value that is equal toEOF
, indicating that there are no more input items to be read.
Note that the
~
operator is not strictly necessary in this case. The following code is equivalent:
while (scanf("%d%d%d%d",&x,&y,&a,&b) != EOF)
This version of the code checks for
EOF
explicitly using the!=
operator, instead of inverting the return value of scanf.
The code
d[i][j] ? s++ : s
is a ternary operator that increments the value ofs
if the value ofd[i][j]
is non-zero, and leavess
unchanged otherwise.
In C++, the ternary operator has the following syntax:
condition ? value_if_true : value_if_false
.
So in this case, the condition is d[i][j], which evaluates to true if the value of d[i][j] is non-zero and false otherwise.
If the condition is true, then the value of
s
is incremented by 1 using the++
operator. If the condition is false, then the value of s is left unchanged (since s is the value_if_false expression).
So the ternary operator can be read as follows: "If the value of
d[i][j]
is non-zero, then increment the value ofs
by 1; otherwise, leave the value ofs
unchanged."
The ternary operator is a compact way to write an if-else statement. The above code could also be written as follows using an if-else statement:
if (d[i][j]) {
s++;
} else {
s = s;
}