Little Jay

[C++] 백준 1065번 - 한수 본문

알고리즘/BOJ

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

Jay, Lee 2021. 9. 5. 16:15

1~99까지의 숫자는 모두 한수 이다.

등차수열을 생각하면 원소를 더하는 경우와 빼는 경우, 공차가 0인 경우 모두 생각하자

#include <iostream>
#include <cmath>
using namespace std;

int calc(int n) {
	int count = 0;
	int first, second, third;

	if (n < 100)
		return n;
	else {
		for (int i = 100; i <= n; i++) {
			first = i / 100;
			second = (i % 100) / 10;
			third = (i % 100) % 10;

			if ((first - second) == (second - third)) {
				count++;
			}
		}
		return (99 + count);
	}
}


int main() {

	int n;
	cin >> n;

	cout << calc(n) << '\n';

	return 0;
}

 

Comments