```
백준 11970번 Fence Painting C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 11970번 풀이
https://www.acmicpc.net/problem/11970
백준 11970번 Fence Painting은
난이도 브론즈 등급의 문제로서
a부터 b까지의 정수를 칠하고
c부터 d까지의 정수를 칠했을 때
칠해진 구간을 계산해주면 되는 문제입니다.
모든 수는 0 부터 100까지의 범위라고 가정을 합니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
방법 1
if else 문으로 풀어보겠습니다.
구간을 하나 하나 if else로 정하기 때문에
머리가 아픕니다.
전체 코드는 다음과 같습니다
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>
int main()
{
// 입력 최적화
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
int32_t farmer_john_start_pos;
ss >> farmer_john_start_pos;
int32_t farmer_john_end_pos;
ss >> farmer_john_end_pos;
ss.seekg(0);
std::getline(std::cin, inputStr);
ss.str(inputStr);
int32_t bessie_start_pos;
ss >> bessie_start_pos;
int32_t bessie_end_pos;
ss>> bessie_end_pos;
int32_t ans;
if (farmer_john_end_pos > bessie_end_pos)
{
if (farmer_john_start_pos > bessie_end_pos)
{
ans = (farmer_john_end_pos - farmer_john_start_pos) + (bessie_end_pos - bessie_start_pos);
}
else
{
if (farmer_john_start_pos > bessie_start_pos)
{
ans = (farmer_john_end_pos - bessie_start_pos);
}
else
{
ans = (farmer_john_end_pos - farmer_john_start_pos);
}
}
}
else
{
if (farmer_john_end_pos < bessie_start_pos)
{
ans = (farmer_john_end_pos - farmer_john_start_pos) + (bessie_end_pos - bessie_start_pos);
}
else
{
if (farmer_john_start_pos > bessie_start_pos)
{
ans = (bessie_end_pos - bessie_start_pos);
}
else
{
ans = (bessie_end_pos - farmer_john_start_pos);
}
}
}
std::cout << ans;
}
방법 2
배열에 칠한것을 설정해주고
하나씩 for문을 돌며 계산을 해줍니다.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>
int main()
{
// 입력 최적화
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
int32_t farmer_john_start_pos;
ss >> farmer_john_start_pos;
int32_t farmer_john_end_pos;
ss >> farmer_john_end_pos;
ss.seekg(0);
std::getline(std::cin, inputStr);
ss.str(inputStr);
int32_t bessie_start_pos;
ss >> bessie_start_pos;
int32_t bessie_end_pos;
ss>> bessie_end_pos;
int32_t ans = 0;
const int32_t fenceSize = 101;
bool fenceArr[fenceSize] = { false };
// 칠한다
for (int32_t i = farmer_john_start_pos; i < farmer_john_end_pos; i++)
{
fenceArr[i] = true;
}
for (int32_t i = bessie_start_pos; i < bessie_end_pos; i++)
{
fenceArr[i] = true;
}
for (int32_t i = 0; i < fenceSize; i++)
{
if (fenceArr[i] == true)
{
ans++;
};
}
std::cout << ans;
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
C++ Primer
Introduction to Algorithms
https://codemasterkimc.tistory.com/35
C++ 이론을 배울수 있는 곳 정리
개요 C++을 배우는 책, 강의, 블로그, 링크 등을 공유합니다. (링크 및 간략한 설명을 하였으나 만약 원작자가 링크를 거는것을 원치 않을 경우 연락주시기 바랍니다.) 서적 https://www.amazon.com/Prime
codemasterkimc.tistory.com
https://codemasterkimc.tistory.com/50
300년차 개발자의 좋은 코드 5계명 (Clean Code)
이번 글을 통해 배워갈 내용 좋은 코드(Clean Code)를 작성하기 위해 개발자로서 생각해볼 5가지 요소를 알아보겠습니다. 개요 좋은 코드란 무엇일까요? 저는 자원이 한정적인 컴퓨터 세상에서 좋
codemasterkimc.tistory.com
'C++ > C++ 알고리즘' 카테고리의 다른 글
| 백준 2525번 오븐시계 C++ 구현해보기 (0) | 2021.10.31 |
|---|---|
| 백준 11971번 속도위반 C++ 구현해보기 (1) | 2021.10.31 |
| 백준 5430번 AC C++ 구현해보기 (0) | 2021.10.30 |
| 백준 2108번 통계학 C++ 구현해보기 (0) | 2021.10.30 |
| 백준 11944번 NN C++ 구현해보기 (0) | 2021.10.30 |