```
백준 11170번 0의 개수 C++ 구현해보기
```

이번 글을 통해 배워갈 내용
- 백준 11170번 풀이
https://www.acmicpc.net/problem/11170
11170번: 0의 개수
N부터 M까지의 수들을 종이에 적었을 때 종이에 적힌 0들을 세는 프로그램을 작성하라. 예를 들어, N, M이 각각 0, 10일 때 0을 세면 0에 하나, 10에 하나가 있으므로 답은 2이다.
www.acmicpc.net
백준 11170번 0의 개수는
난이도 쉬움 등급의 문제로서
N 부터 M 까지 숫자들에서 자릿수에 0이 몇번 나왔는지 구하는 문제이다.
30분 정도 위에 링크를 방문하셔서 풀어보시고
안풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.
테스트 케이스만 큼 반복해서 작업을 하기 때문에
테스트 케이스 입력을 받고
for문을 돌려서 작업을 한다.
각 숫자를 입력받고
입력 받은 숫자 사이에 숫자만큼 반복해서 0을 찾는다
int count0()
{
int retVal = 0;
for (int i = num1; i <= num2; i++)
{
std::string tempStr = std::to_string(i);
for (char letter : tempStr)
{
if (letter == '0')
{
retVal++;
}
}
}
return retVal;
}
전체 코드는 다음과 같습니다
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
class CCount0 {
private:
int num1;
int num2;
public:
CCount0()
{
}
~CCount0()
{
}
/* 입력 */
void getUserInput()
{
std::string inputStr;
std::getline(std::cin, inputStr);
std::stringstream ss(inputStr);
ss >> num1;
ss >> num2;
}
/* 두개의 멤버 변수 num1과2에 대한 0갯수 세기 */
void printCount0()
{
const int answer = count0();
std::cout << answer << "\n";
}
int count0()
{
int retVal = 0;
for (int i = num1; i <= num2; i++)
{
std::string tempStr = std::to_string(i);
for (char letter : tempStr)
{
if (letter == '0')
{
retVal++;
}
}
}
return retVal;
}
};
int main()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
int testCaseNum;
std::cin >> testCaseNum;
std::cin.ignore();
for (int i = 0; i < testCaseNum; i++)
{
CCount0* cCount0 = new CCount0();
cCount0->getUserInput();
cCount0->printCount0();
delete cCount0;
}
}
읽어주셔서 감사합니다
무엇인가 얻어가셨기를 바라며
오늘도 즐거운 코딩하시길 바랍니다 ~ :)
참조 및 인용
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++ 알고리즘' 카테고리의 다른 글
| 백준 2163번 초콜릿자르기 C++ 구현해보기 (0) | 2021.09.04 |
|---|---|
| 백준 11024번 더하기4 C++ 구현해보기 (0) | 2021.09.04 |
| 백준 2712번 단위 변환 C++ 구현해보기 (0) | 2021.09.04 |
| 백준 2711번 인덱스 지운 문자열 C++ 구현해보기 (0) | 2021.09.04 |
| 백준 21567번 숫자의 개수2 C++로 구현해보기 (0) | 2021.09.03 |