반응형
문제
https://www.acmicpc.net/problem/1065
1065번: 한수
어떤 양의 정수 X의 각 자리가 등차수열을 이룬다면, 그 수를 한수라고 한다. 등차수열은 연속된 두 개의 수의 차이가 일정한 수열을 말한다. N이 주어졌을 때, 1보다 크거나 같고, N보다 작거나
www.acmicpc.net
해설
1~99까지는 무조건 등차수열이 될 것이다. 비교할 3번째 항이 없기 때문이다. 100 이상은 각 자릿수를 분해하여 계산해 보면 된다.
#include <iostream>
using namespace std;
int hansu(int x) {
int res = 0, th, h, t, n;
for (int i = 1; i <= x; i++) {
if (i >= 1000) {
th = 1;
h = int((i - th * 1000) / 100);
t = int((i - th * 1000 - h * 100) / 10);
n = int(i - th * 1000 - h * 100 - t * 10);
if ((n - t) == (t - h))
if ((n - t) == (h - th))
res += 1;
}
else if (i >= 100) {
h = int(i / 100);
t = int((i - h * 100) / 10);
n = int(i - h * 100 - t * 10);
if ((n - t) == (t - h))
res += 1;
}
else {
res += 1;
}
}
return res;
}
int main() {
int x;
cin >> x;
cout << hansu(x);
}
반응형