Programming/Baekjoon

백준 11170 c++

fishersheep 2022. 1. 18. 13:00
반응형

백준: 0의개수

난이도: 실버5

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
using namespace std;

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

	int n, m;	
	int t;	//테스트케이스의 수
	int count = 0;	//0을 세는 변수
	string str;	
	
	cin >> t;
	int* arr = new int[t];	//여러개의 결과값을 출력할 배열

	for (int i = 0; i < t; i++)	//테스트케이스 만큼 반복
	{
		cin >> n >> m;
		for (int j = n; j <= m;j++)	//n부터 m사이를 반복
		{
			str = (std::to_string(j));	//j를 문자열로 변환
			for (char temp : str)	//문자열에서 '0'이 있는지 반복문으로 확인하고
			{
				if (temp == '0')
					count++;	//있으면 count에 1을더한다.
			}
		}
		arr[i] = count;	//1개의 테스트케이스가 끝나면 count를 저장하고
		count = 0;	//초기화
	}

	for (int i = 0; i < t; i++)
		cout << arr[i] << '\n';
	

	delete[] arr;
	return 0;
}
반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 11651 c++  (0) 2022.01.18
백준 11650 c++  (0) 2022.01.18
백준 11047 동전0 [c++]  (0) 2022.01.17
백준 5800 성적 통계 [c++]  (0) 2022.01.17
백준 10773 제로 [c++]  (0) 2022.01.17