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())겠. --










