1. C++ ¶
~cpp
#include <iostream>
using namespace std;
void input();
int process();
int findMaxCycle(int aNum, int aCount);
void output(int aMaxCycle);
int inputNum[2];
int main()
{
input();
int max = process();
output(max);
return 0;
}
void input()
{
cout << "<< ";
cin >> inputNum[0] >> inputNum[1];
}
int process()
{
int interNum = inputNum[0];
int maxCycle;
int temp;
int count = 1;
while (interNum <= inputNum[1])
{
count = 1;
temp = findMaxCycle(interNum, count);
if (maxCycle < temp)
maxCycle = temp;
interNum++;
}
return maxCycle;
}
int findMaxCycle(int aNum, int aCount)
{
if (aNum == 1)
return aCount;
if (aNum % 2)
aNum = 3 * aNum + 1;
else
aNum = aNum / 2;
aCount++;
findMaxCycle(aNum, aCount);
}
void output(int aMaxCycle)
{
cout << aMaxCycle << endl;
}
2. Python ¶
2005.1.9
~cpp
import unittest
import psyco
class ThreeNPlusOne:
def __init__(self):
self.cycleLength = 0
self.cycleDic = {}
def compute(self, num):
self.cycleLength = 0
while (num != 1):
self.cycleLength += 1
if num % 2:
num = 3 * num + 1
else:
num /= 2
if str(num) in self.cycleDic:
return self.cycleLength + self.cycleDic[str(num)]
self.cycleLength += 1
return self.cycleLength
def computeAll(self, num1, num2):
maxLength = 0
for n in range(num1, num2+1):
length = self.compute(n)
self.cycleDic[str(n)] = length
if length > maxLength:
maxLength = length
return maxLength
class ThreeNPlusOneTest(unittest.TestCase):
def setUp(self):
self.u = ThreeNPlusOne()
def testOneCal(self):
num = 22
expected = 16
self.assertEquals(expected, self.u.compute(num))
def testTwoCal(self):
self.assertEquals(20, self.u.computeAll(1,10))
self.assertEquals(125, self.u.computeAll(100, 200))
self.assertEquals(89, self.u.computeAll(201, 210))
self.assertEquals(174, self.u.computeAll(900, 1000))
self.assertEquals(525, self.u.computeAll(1, 999999))
def main():
n = raw_input()
n1, n2 = n.split()
o = ThreeNPlusOne()
result = o.computeAll(int(n1), int(n2))
print n1, n2, result
if __name__ == '__main__':
psyco.full()
unittest.main(argv=('','-v'))
#main()
3. Python ¶
2005.1.13
~cpp
import unittest
import psyco
cycleDic = dict()
def compute(num):
cycleLength = 1
while (num != 1):
num = (num %2 and 3 * num + 1) or num / 2
if num in cycleDic:
return cycleLength + cycleDic[num]
cycleLength += 1
return cycleLength
def computeAll(num1, num2):
maxLength = 0
for n in range(num1, num2+1):
length = compute(n)
cycleDic[n] = length
if length > maxLength:
maxLength = length
return maxLength
class ThreeNPlusOneTest(unittest.TestCase):
def testOneCal(self):
num = 22
expected = 16
self.assertEquals(expected, compute(num))
def testTwoCal(self):
self.assertEquals(20, computeAll(1,10))
self.assertEquals(125, computeAll(100, 200))
self.assertEquals(89, computeAll(201, 210))
self.assertEquals(174, computeAll(900, 1000))
self.assertEquals(525, computeAll(1, 999999))
def main():
n = raw_input()
n1, n2 = n.split()
result = computeAll(int(n1), int(n2))
print n1, n2, result
if __name__ == '__main__':
psyco.full()
unittest.main(argv=('','-v'))
#main()
4. 드 ¶
력 0과 1000000 값 갖는 다. 1과 999999를 력 경 몇 내 답 나까. Python로 4 내를 목로 구다. 만 만 만 결과가 나 다. 깝게 더 묘 떠르 는다 --
http://bioinfo.sarang.net/wiki/AlgorithmQuiz_2f3Plus1 yong27님 드를 보다. 가 말 깔끔다. 가 빨라 그 가며 난 던 드를 다. 나 목 0.001라 빠르게 결과를 력는 것다. 기 래 다. 두 부 다. 나는 래 멤변를 고 변 경데 그런 모르겠다. 둘는 cycleDic key를 문 로 바꾼 부다. 난 구 무때문 문로 변 key로 만들는 모르겠다. --
멋 드
~cpp num = (num %2 and 3 * num + 1) or num / 2
~cpp i,j = map(int,raw_input().split())둘다 무 미 모르겠다. --










