C++/C++ 알고리즘

백준 10807번 개수 세기 C++ 구현해보기

kimc 2021. 9. 25. 11:52

```

백준 10807번 개수 세기 C++ 구현해보기

```

 

이번 글을 통해 배워갈 내용

  1.  백준 10807번 풀이

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

 

10807번: 개수 세기

첫째 줄에 정수의 개수 N(1 ≤ N ≤ 100)이 주어진다. 둘째 줄에는 정수가 공백으로 구분되어져있다. 셋째 줄에는 찾으려고 하는 정수 v가 주어진다. 입력으로 주어지는 정수와 v는 -100보다 크거

www.acmicpc.net

 

 

 

백준 10807번 개수 세기는 

난이도 브론즈 등급의 문제로서

 

N개의 정수가 주어질 때 특정 정수가 몇 개인지 세는 문제입니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


 

숫자의 개수를 입력을 받고

입력받은 숫자만큼 반복문을 돌면서

스택에 입력값을 넣어주고

 

비교할 숫자를 입력받은 뒤에

스택에서 숫자를 하나씩 뺀 다음

 

각 숫자에 대해서

스택이 빌 때 까지

비교된 숫자가 동일하면

출력 값에 1을 더해주면

 

되는 문제입니다.

 

아래에는 전체 코드입니다.

#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);

	int32_t testCase;
	std::cin >> testCase; 
	std::cin.ignore();

	std::string inputStr;
	std::string inputStrV;
	std::getline(std::cin, inputStr);
	std::getline(std::cin, inputStrV);
	std::stringstream ss(inputStr);

	std::stack<int32_t> myStack;

	for (int32_t i = 0; i < testCase; i++)
	{
		int32_t tempVal;
		ss >> tempVal;
		myStack.push(tempVal);
	}

	int32_t vCnt = 0;
	int32_t vNum = stoi(inputStrV);

	while (myStack.empty() == false)
	{
		if (vNum == myStack.top())
		{
			vCnt++;
		};

		myStack.pop();
	}

	std::cout << vCnt;
}

 

 

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 

참조 및 인용

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

 


 

728x90