## 문제
BOJ 1065 한수
## solution
양의 정수 x의 각 자리가 등차수열을 이룬다면 그 수를 한수라고 한다. n이 주어질때 1보다 크거나 같고, n보다 작거나 같은 한수의 개수를 출력하는 프로그램 작성하기.
x의 각 자리가 등차수열을 이루는지 판별하고, 1부터 n까지 개수를 세면 된다. x를 list로 바꾸고 각 자리 차가 일정한지 보면 된다.
N <= 1000 조건이 있다.
## CODE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def arithmetic(num): | |
# 한수인지 판단하기 | |
step_return = True | |
list_num = list(str(num)) | |
if len(list_num) == 1: | |
step_return = True | |
else: | |
num_step = int(list_num[1]) - int(list_num[0]) | |
for idx in range(1,len(list_num)): | |
tmp_step = int(list_num[idx]) - int(list_num[idx - 1]) | |
if tmp_step != num_step: | |
step_return = False | |
return step_return | |
def main(): | |
input_num = int(input()) | |
result_count = 0 | |
for k in range(1,input_num + 1): | |
if arithmetic(k) == True: | |
result_count += 1 | |
print(result_count) | |
return None | |
if __name__ == '__main__': | |
main() |
내 풀이
```python
NumHan = 0
for i in range(1, int(input())+ 1) :
NumHan = NumHan + 1 if i < 100 or i//10%10*2==i%10+i//100 else NumHan
print(NumHan)
```
다른 사람의 풀이인데, 주어지는 숫자가 1000보다 작으므로 10으로 나누는(몫을 구하는) 등의 조작을 통해서 각 자리 숫자만 뽑아서 판단할수 있다.
이렇게 보니까 내 풀이가 미련하게 보이네...
다시보니까 100보다 작은 경우 모두 한수에 해당한다. 한자리 수, 두자리 수의 경우 그냥 등차수열이 되므로 판단할 필요가 없다.
0 댓글