[백준] 1065번 한수 / C++

#문제

백준 1065번 한수

#풀이

#include <iostream>
#include <string>

using namespace std;

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

	int n;
	cin >> n;

	int cnt = 0;

	if (n < 100)
	{
		cnt = n;
	}
	else
	{
		cnt = 99;

		for (int i = 100; i <= n; ++i)
		{
			int hun = i / 100;
			int ten = (i / 10) % 10;
			int one = i % 10;

			if ((hun - ten) == (ten - one))
			{
				cnt++;
			}
		}
	}

	cout << cnt;

	return 0;
}

#정리

각 자리가 등차수열을 이룬다면, 그 수를 한수라고 한다. 100 이하인 경우에는 비교가능한 자릿수가 없기 때문에 바로 출력하고, 100 이상일 경우에는 각 자리수의 차를 비교하여 카운트하여 출력한다.




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • [백준] 2563번 색종이 / C++
  • [백준] 1018번 체스판 다시 칠하기 / C++
  • [백준] 1764번 듣보잡 / C++
  • [백준] 1269번 대칭 차집합 / C++
  • [백준] 15652번 N과 M (4) / C++