Algorithm/Brute Force

BaekJoon 1065 한수

GirlFriend_Yerin 2018. 12. 28. 01:14

Link : https://www.acmicpc.net/problem/1065

소스 결과 : 1984 KB / 0 ms

출처 : BackJoon

 

설명

생각보다 쉬운 문제

일단 범위가 자연수 한정에 1000 이하 이지만

실질적으로 1 ~ 99 까지는 무조건 등차수열이고

1000은 등차수열이 아니니 100 ~ 999 까지만 계산하면 되는 문제였다.

 

소스코드

#include <iostream>

using namespace std;

int main()
{
	int n;
	int count = 0;

	cin >> n;

	if (n == 1000) // 1000 is not arithmetic sequence
		n = 999;

	if (n < 100) // under 100 is arithmetic sequence
		count = n;
	else
	{
		count = 99;

		for (int i = 100; i < n + 1; i++)
		{
			int number = i;
			int digit[3];
			int j = 0;
			while (number)
			{
				digit[j++] = number % 10;
				number /= 10;
			}
		
			if (digit[0] - digit[1] == digit[1] - digit[2])
				count++;
		}
	}

	cout << count;

	return 0;
}