본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 11970 : Fance Painting

반응형

https://www.acmicpc.net/problem/11970

 

11970번: Fence Painting

Several seasons of hot summers and cold winters have taken their toll on Farmer John's fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is actually remarkably proficient at paintin

www.acmicpc.net

간단 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

정답을 출력할 ans, 칠해진 paint상황을 저장할 fence, 입력받을 일차원 변수 a를 선언한 후 a에 4개의 범위를 입력받습니다.

📔 풀이과정

1. a[0] <= i < a[1], a[2] <= i < a[3] 구역만큼 fence에 칠해줍니다

2. 이후 울타리 길이만큼 for loop를 수행하며 칠해진 구간의 길이를 세줍니다.

📔 정답출력

ans를 출력해줍니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int ans, fence[101], a[4];
int main(){
  cin >> a[0] >> a[1] >> a[2] >> a[3];
  for(int i = a[0]; i < a[1]; i++) fence[i]++;
  for(int i = a[2]; i < a[3]; i++) fence[i]++;
  for(int i = 0; i <= 100; i++){
    if(fence[i]) ans ++;
  }
  cout << ans;
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.