C++/C++ 알고리즘

백준 17174번 전체 계산 횟수 C++로 구현해보기

kimc 2021. 8. 31. 23:35

```

백준 17174번 전체 계산 횟수 C++로 구현해보기

```

 

이번 글을 통해 배워갈 내용

  1.  백준 17174번 풀이

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

 

17174번: 전체 계산 횟수

첫 번째 줄에 환전한 금액 N과 묶음의 크기 M이 주어진다. (2 ≤ N ≤ 100,000, 2 ≤ M ≤ N)

www.acmicpc.net

 

백준 17174번 전체 계산 횟수는

난이도 쉬움 등급의 문제로서

 

정수 N 과 정수 M이 주어질때

 

N을 세고 N을 M묶음씩 세고 N을 M묶음으로 묶은 것을 또 M묶음으로 센다.

나누기 M을 해서 더하는게 0이 될때까지 반복한다

 

답 = N/1 + N/M + N/M/M .... + 0


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

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


 

N과 M을 입력받고

	void getInputFromUser()
	{
		std::string inputString;
		std::getline(std::cin, inputString);
		std::stringstream ss(inputString);
		ss >> N;
		ss >> M;
	}

더하는 숫자가 0이 아닐 때 까지 계속 더해준다.

	void findAnswer()
	{
		int answer = N;
		int tempVal = N;

		while (true)
		{
			tempVal = tempVal / M;

			if (tempVal == 0)
			{
				break;
			}
			else
			{
				answer = answer + tempVal;
			}
		}

		std::cout << answer;
	}

 

 

 

전체 코드는 다음과 같습니다

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include<numeric>

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

class CTotalCount
{
private:
	/* 지폐수 */
	int N;
	/* 묶음 */
	int M;

public:
	CTotalCount()
	{
	}

	~CTotalCount()
	{
	}

	void getInputFromUser()
	{
		std::string inputString;
		std::getline(std::cin, inputString);
		std::stringstream ss(inputString);
		ss >> N;
		ss >> M;
	}

	void findAnswer()
	{
		int answer = N;
		int tempVal = N;

		while (true)
		{
			tempVal = tempVal / M;

			if (tempVal == 0)
			{
				break;
			}
			else
			{
				answer = answer + tempVal;
			}
		}

		std::cout << answer;
	}
};

int main()
{
	std::cin.tie(NULL);
	std::ios::sync_with_stdio(false);

	CTotalCount* cTotalCount = new CTotalCount();

	cTotalCount->getInputFromUser();
	cTotalCount->findAnswer();
}

 

 

읽어주셔서 감사합니다

 

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

 

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

 

참조 및 인용

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