***Note: Please keep programs under 7000 characters in length. Thank you
Class Name: SquareDigits
Method Name: smallestResult
Parameters: int
Returns: int
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.
repeatedly applying S to x. That is: S(x), S(S(x)), S(S(S(x))), etc...
For example, repeatedly applying S to 37:
S(37)=3*3+7*7=58.
S(58)=5*5+8*8=89.
S(89)=145.
S(145)=42.
S(42)=20.
S(20)=4.
S(4)=16.
S(16)=37.
Note this sequence will repeat so we can stop calculating now and:
T(37)={58,89,145,42,20,4,16,37}.
However, note T(x) may not necessarily contain x.
method takes an int, n, as a parameter and returns the smallest int, x, such
that T(x) contains n.
int smallestResult(int n);
If n=0: S(0) = 0, so T(0)={0}, so the method should return 0.
S(11)=1*1+1*1=2, so T(11) contains 2, and the method should return 11.
S(7)=49.
S(49)=97.
S(97)=130.
S(130)=10.
S(10)=1.
and it starts to repeat...
so T(7) is {49,97,130,10,1}, which contains 10, and the method should return 7.
n=19 -> x=133
n=85 -> x=5
n=112 -> x=2666
Definition
Class:
SquareDigits
Method:
smallestResult
Parameters:
int
Returns:
int
Method signature:
int smallestResult(int param0)
(be sure your method is public)
대충 얘기를 하자면 X(11)= 1*1 + 1*1 = 2 라는 X 함수가 존재합니다. 그런데 이 함수는 중첩적으로 계속 하여 호출 될 수 있습니다. 대충 예를 들면 X(11) = 2, X(2) = 4, X(4) = 16 ... 등등으로 계속 호출 될 수 있겠죠.
그러다가 이 함수 호출에 있어서 X(A)=Y 형태 중 A가 이미 호출되었을 때는 해당 N 숫자에 대한 집합에 들어가 있으므로 결국 T(11) = { 2, 4, 16 .. } 형태의 집합으로 나타낼 수 있습니다.
중요한 것은 T(11)로 표현되는 집합에 11이라는 숫자가 들어가 있는가 하는 점입니다. 들어가 있다면 11을 리턴하게 되고, 포함되어있지 않으면 T(12)를 작업해보고, 그 결과에 따라 13, 14로 늘어날 수 있습니다. 그리고 제일 작은 숫자를 리턴하면 됩니다.
이 번엔 제대로 검사를 하긴 했습니다. 그런데 INT_MAX 라는 범위가 있다보니 199까지 최대 숫자를 측정하여 배열을 이용한 O(1)시간의 검색이 되어야 하나, 손해를 약간 보더라도 set에 대한 find 검색작업을 수행하도록 구성하였습니다.
포인트가 안 좋은건 속도 때문일까요?^^. 그냥 풀어본 것에 의미를 가지도록 하겠습니다.
"TopCoder" 분류의 다른 글
| TopCoder - Tournament - Inv 2001 R1 (4) | 2008/03/05 |
| TopCoder - 전 세계 프로그래머들과 경쟁하기 (0) | 2008/02/18 |








댓글을 달아 주세요