몇달째 Python만 손대다보니 C, C++ 문법조차 다 잊어버린 상황.
다시금 C, C++ 감을 찾기위해 Programming Challenges 책을 꺼내들었다.
심심할때마다 처음부터 차근차근 하나씩 풀어가기 위해.
처음으로 풀어본 문제는 예전에도 이미 한번 풀어본 적 있는 '3n+1 Problem'.
아직 이 문제를 효율적으로 풀 수 있는 알고리즘을 제대로 이해하지 못해서 직관적인 방법으로 풀었던니 실행시간이 너무 길다.(최단시간 0.008초, 내껀 0.828초...-_-)
효율적인 알고리즘을 공부 한 뒤에 다시 풀어봐야지...~_~

* 문제

* C 버전 소스코드

* C++ 버전 소스코드
문제 : http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110502&format=html

Status : Solved
#include <iostream> using namespace std; unsigned int reverse(unsigned int input); int main(void) { unsigned int numberOfTestcase, input, i; cin >> numberOfTestcase; for(i = 0; i < numberOfTestcase; i++) { int count = 0; cin >> input; if(input == reverse(input)) { cout << 0 << " " << input << endl; } else { while((input = input + reverse(input)) != reverse(input)) { count++; } cout << ++count << " " << input << endl; } } return 0; } unsigned int reverse(unsigned int input) { unsigned int result = 0; while(input > 0) { result *= 10; result += input % 10; input = input / 10; } return result; }

+ Recent posts