티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <stdio.h> #include <math.h> void main() { double input; int tempInput = 0; int count = 0; int temp = 0; int i; for (i = 1000000; i >= 1; --i) { input = i; for (;;) { if (fmod(input, 2) == 0) input /= 2; else input = input * 3 + 1; ++count; if (input == 1) { if (temp < count) { temp = count; tempInput = i; } break; } } printf("%d\t%d\n", i, count); count = 0; } printf("%d %d", temp, tempInput); } | cs |
처음엔 input을 int형으로 선언하고 돌렸는데
input이 4바이트 정수 최대 범위를 넘어가는지 중간에 멈춰버리길래
double형으로 바꿔줬더니 되었다.
double형으로 바꿔주면서 조건식도 %에서 fmod()로 바꿔주어야 했다.
시간이 좀 걸리는 알고리즘으로(커서 5번 깜빡) 수정이 불가피할 것이다.
'Algorithm > Project Euler' 카테고리의 다른 글
프로젝트 오일러 2번 (0) | 2017.05.19 |
---|---|
프로젝트 오일러 20번 (0) | 2017.05.19 |
프로젝트 오일러 16번 (0) | 2017.05.18 |
프로젝트 오일러 3번 (0) | 2017.05.17 |
프로젝트 오일러 11번 (0) | 2017.05.15 |
댓글