[ACM]Q100: The 3n + 1 problem
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q100
{
class Program
{
static private uint _i, _j;
static private bool _itof, _jtof;
static void Main(string[] args)
{
for (; ; )
{
labeli: Console.Write("i>>>");
_itof = uint.TryParse(Console.ReadLine(), out _i);
if (!_itof||_i==0) goto labeli;
labelj: Console.Write("j>>>");
_jtof = uint.TryParse(Console.ReadLine(), out _j);
if (!_jtof||_j>=1000000) goto labelj;
Console.WriteLine("i={0}, j={1} ,cycle-length={2}", _i, _j, MaxCL(_i, _j));
}
}
static public int MaxCL(uint x, uint y)
{
int countCL = 1; int tempCL;
if (x < y)
{
uint temp;
temp = x;
x = y;
y = temp;
}
for (uint i = y; i <= x; i++)
{
tempCL = countCL;
countCL = 1;
for (uint j = i; j != 1; )
{
if (j % 2 == 0)
{ j /= 2; countCL++; }
else
{ j = 3 * j + 1; countCL++; }
}
countCL = tempCL > countCL ? tempCL : countCL;
}
return countCL;
}
}
}