```
백준 5054번 주차 C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 5054번 풀이
- 간단한 클래스 및 벡터 연습
https://www.acmicpc.net/problem/5054
5054번: 주차의 신
첫째 줄에 테스트 케이스의 개수 t가 주어진다. (1 ≤ t ≤ 100) 모든 테스트 케이스는 두 줄로 이루어져 있다. 첫째 줄에는 선영이가 방문할 상점의 수 n이 주어지며 (1 ≤ n ≤ 20), 둘째 줄에는 상점
www.acmicpc.net
백준 5054번은
난이도 브론즈 등급의 문제로서
테스트 케이스만큼 반복되면서 입력을 받는다.
각 입력 첫줄에 상점의 숫자가 주어지고
둘째 줄에 상점의 위치가 주어진다.
상점을 모두 방문하는데 가장 짧은 위치로 가면 되는 문제이다.
이때 걸어야 하는 최소 값을 구해주면 되는데
이는 수학적으로 각 테스트 케이스에서
최댓값 - 최솟값을 해주고 곱하기 2를 해주면 된다.
왜냐하면 중간에 나머지 가게들이 있기 때문이다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
요즘에는 객체지향적으로 풀려고 노력하다 보니
코드가 조금은 길어지는것 같다.
일단 각 상점의 위치를 담는
CShopList 클래스를 선언한다.
이안에는 상점의 위치가 들어가는 shopVec이 들어있다.
class CShopList
{
private:
std::vector<int32_t>shopVec;
public:
CShopList(std::vector<int32_t> shopVec)
{
this->shopVec = shopVec;
}
const std::vector<int32_t> getshopVec()
{
return shopVec;
}
};
CParkingDistanceCalc 함수에는 위에 선언한 가게 정보들을 담은 CShopList를 여러 개 담는 shopListVec이 있고
class CParkingDistanceCalc
{
private:
int32_t testCaseNum;
std::vector<CShopList> shopListsVec;
public:
CParkingDistanceCalc();
~CParkingDistanceCalc();
void getUserInputTestCase();
void getUserInputShopListVec();
void printMinWalkingDistance();
};
메인에서 실행한다.
테스트케이스를 입력받고
테스트 케이스만큼 샵 리스트를 입력받은 뒤
최소 이동거리를 각 테스트 케이스만큼 출력한다.
(의사도를 잘 나타냈는지 고민이다)
int main()
{
CParkingDistanceCalc * cParkingDistanceCalc = new CParkingDistanceCalc();
cParkingDistanceCalc->getUserInputTestCase();
cParkingDistanceCalc->getUserInputShopListVec();
cParkingDistanceCalc->printMinWalkingDistance();
delete cParkingDistanceCalc;
}
각 함수의 자세한 코드는 전체 코드에 나와있다.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <regex>
#include <unordered_map>
using namespace std;
class CShopList
{
private:
std::vector<int32_t>shopVec;
public:
CShopList(std::vector<int32_t> shopVec)
{
this->shopVec = shopVec;
}
const std::vector<int32_t> getshopVec()
{
return shopVec;
}
};
class CParkingDistanceCalc
{
private:
int32_t testCaseNum;
std::vector<CShopList> shopListsVec;
public:
CParkingDistanceCalc();
~CParkingDistanceCalc();
void getUserInputTestCase();
void getUserInputShopListVec();
void printMinWalkingDistance();
};
int main()
{
CParkingDistanceCalc * cParkingDistanceCalc = new CParkingDistanceCalc();
cParkingDistanceCalc->getUserInputTestCase();
cParkingDistanceCalc->getUserInputShopListVec();
cParkingDistanceCalc->printMinWalkingDistance();
delete cParkingDistanceCalc;
}
CParkingDistanceCalc::CParkingDistanceCalc()
{
testCaseNum = 0;
}
CParkingDistanceCalc::~CParkingDistanceCalc()
{
}
void CParkingDistanceCalc::getUserInputTestCase()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> testCaseNum;
}
void CParkingDistanceCalc::getUserInputShopListVec()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
for (int32_t i = 0; i < testCaseNum; i++)
{
std::string numOfShopsStr;
std::string shopListStr;
std::getline(std::cin, numOfShopsStr);
int32_t numOfShops;
std::istringstream(numOfShopsStr) >> numOfShops;
std::vector<int32_t> shopVec;
std::getline(std::cin, shopListStr);
std::stringstream ss(shopListStr);
for(int32_t i =0;i< numOfShops;i++)
{
int32_t shopLocation;
ss >> shopLocation;
shopVec.push_back(shopLocation);
}
CShopList cShopList = { shopVec };
shopListsVec.push_back(cShopList);
}
}
void CParkingDistanceCalc::printMinWalkingDistance()
{
for (auto shopListIter : shopListsVec)
{
int32_t maxLocation = 0;
int32_t minLocation = INT_MAX;
for (auto shopIter : shopListIter.getshopVec())
{
if (maxLocation < shopIter)
{
maxLocation = shopIter;
}
if (minLocation > shopIter)
{
minLocation = shopIter;
}
}
int32_t minDistance = (maxLocation - minLocation) * 2;
std::cout << minDistance << "\n";
}
}
//https://stackoverflow.com/questions/7663709/how-can-i-convert-a-stdstring-to-int
이번 구현을 통해
고찰한 부분이 stoi나 atoi를 안 쓰고
std::istringstream(numOfShopsStr) >> numOfShops;
와 같이 스트링을 숫자로 변환한 점이다.
이는 여기를 참조하면 좋다.
https://stackoverflow.com/questions/7663709/how-can-i-convert-a-stdstring-to-int
How can I convert a std::string to int?
Just have a quick question. I've looked around the internet quite a bit and I've found a few solutions but none of them have worked yet. Looking at converting a string to an int and I don't mean A...
stackoverflow.com
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
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++ 알고리즘' 카테고리의 다른 글
| 백준 5522번 카드게임 C++ 구현해보기 (0) | 2021.10.24 |
|---|---|
| 백준 5052번 전화번호 목록 C++ 구현해보기 (0) | 2021.10.16 |
| 백준 7510번 고급수학 C++ 구현해보기 (0) | 2021.10.14 |
| 백준 5800번 성적통계 C++ 구현해보기 (0) | 2021.10.11 |
| 백준 2502번 떡먹는 호랑이 C++ 구현해보기 (0) | 2021.10.07 |