[백준] 1065번 한수 / C++
#문제
#풀이
#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: