```
백준 12603번 Store Credit (Small) C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 12603번 Store Credit (Small) C++ 풀이
- 백준 12604번 Store Credit (Large) C++ 풀이
https://www.acmicpc.net/problem/12603
12603번: Store Credit (Small)
You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solu
www.acmicpc.net
https://www.acmicpc.net/problem/12604
12604번: Store Credit (Large)
You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solu
www.acmicpc.net
백준 12603번 Store Credit(small)는
난이도 브론즈 등급의 문제로서
테스트 케이스만큼
C 가진돈을 받고
P 상점에 있는 아이템의 개수를 받고
P의 개수만큼 아이템의 가격을 한 줄로 입력받습니다.
케이스마다
가진돈으로 살 수 있는 아이템의 인덱스를 구해주면 되는 문제입니다.
아이템은 두 개 살 수 있으며
가진돈을 다 써야 하는 게 조건입니다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
항상 그렇듯 입력을 받고
브루트 포스로
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>
namespace kimc::lib {
std::string find_two_items(int32_t amountOfCredit, int32_t numberOfItems, std::vector<int32_t>&itemVec)
{
std::string outputStr = "";
for (size_t idx1 = 0; idx1 < numberOfItems; idx1++)
{
for (size_t idx2 = 0; idx2 < numberOfItems; idx2++)
{
if (idx1 != idx2)
{
const int32_t creditLeft = amountOfCredit - (itemVec[idx1] + itemVec[idx2]);
if (creditLeft == 0)
{
outputStr = std::to_string(idx2 + 1) + " " + std::to_string(idx1 + 1);
break;
}
}
}
}
return outputStr;
}
}
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 caseCount;
ss >> caseCount;
ss.clear();
ss.seekg(0);
for (int32_t i = 0; i < caseCount; i++)
{
std::getline(std::cin, inputStr);
ss.str(inputStr);
int32_t amountOfCredit;
ss >> amountOfCredit;
ss.clear();
ss.seekg(0);
std::getline(std::cin, inputStr);
ss.str(inputStr);
int32_t numberOfItems;
ss >> numberOfItems;
ss.clear();
ss.seekg(0);
std::getline(std::cin, inputStr);
ss.str(inputStr);
std::vector<int32_t> itemVec;
int32_t item;
while (ss >> item)
{
itemVec.push_back(item);
}
ss.clear();
ss.seekg(0);
std::string outputStr = "";
outputStr += "Case #" + std::to_string(i+1) + ": ";
outputStr += kimc::lib::find_two_items(amountOfCredit, numberOfItems, itemVec);
outputStr += "\n";
std::cout << outputStr;
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩 하시길 바랍니다 ~ :)
참조 및 인용
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++ 알고리즘' 카테고리의 다른 글
| 백준 12595번, 백준 12596번 Odd Man Out C++ 구현해보기 (0) | 2021.11.01 |
|---|---|
| 백준 12607번, 12608번 T9 Spelling C++ 구현해보기 (0) | 2021.11.01 |
| 백준 12605번, 12606번 단어순서 뒤집기 C++ 구현해보기 (1) | 2021.10.31 |
| 백준 17608번 막대기 C++ 구현해보기 (0) | 2021.10.31 |
| 백준 2525번 오븐시계 C++ 구현해보기 (0) | 2021.10.31 |