algorithm the stern brocot number system - andstudy/forge GitHub Wiki

Mastojun

๋ฌธ์ œ ํ’€์ด

  • ๊ทธ๋ž˜ํ”„๋ฅผ ์ž˜ ๋“ค์—ฌ๋‹ค ๋ณด๋ฉด ์ˆซ์ž์˜ ๋ฐฐ์—ด์— ๊ทœ์น™์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ’€์ด์—” "์ฐธ๊ณ ๋กœ, ์œ ๋ฆฌ์ˆ˜ m/n๊ณผ p/q์˜ ๋Œ€์†Œ๋ฅผ ๋น„๊ตํ•˜๋Š”๋ฐ ์‹ค์ œ๋กœ m์„ n์œผ๋กœ, p๋ฅผ q๋กœ ๋‚˜๋ˆ„์–ด ์‹ค์ˆ˜ ๋ณ€์ˆ˜์— ๋Œ€์ž…ํ•˜๋Š” ์ผ์€ ํ•˜์ง€ ๋ง์ž." ๋ผ๊ณ  ๋˜์–ด ์žˆ๋Š”๋ฐ.... ์ „ ์‹ค์ œ๋กœ ๋‚˜๋ˆ„์—ˆ์Šต๋‹ˆ๋‹ค ๋‹จ float์œผ๋กœ ํ•˜๋ฉด ์ œ๋Œ€๋กœ ๋น„๊ต๊ฐ€ ๋ ๊บผ ๊ฐ™์ง€ ์•Š์•„ double๋กœ ํ–ˆ์–ด์š”.

Programming-Challenges ๋กœ๋ด‡์˜ ๋ฌธ์ œ์ 

  • UVa์—์„œ๋Š” Accepted๋ฅผ ๋ฐ›์•˜์ง€๋งŒ (UVa์—์„œ๋Š” Solved๋ฅผ Accepted๋ผ๊ณ  ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค) PC์—์„œ๋Š” Presentation Error๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์ €๋ฒˆ์—๋„ ๋น„์Šทํ•œ ๋ฌธ์ œ๋กœ ๋ฉ”์ผ ๋ณด๋‚ธ์ ์ด ์žˆ์—ˆ๋Š”๋ฐ MD5๋ฌธ์ œ๋ผ๊ณ  ํ•˜๋˜๊ฑฐ ๊ฐ™์€๋ฐ. ์ด๋ฒˆ์—๋„ ๋ฉ”์ผ ๋ณด๋ƒˆ์œผ๋‹ˆ ๋ช‡์ผ ์ด๋‚ด๋กœ ๊ณ ์ณ์งˆ๊บผ๋ผ ์ƒ๊ฐํ•ด์š”.

๋‹ต์žฅ์ด ์™”์Šต๋‹ˆ๋‹ค.

     Hello,
     there were a mistake with the md5sum check, as some time in the past we fixed the 'correct' output and the people in charge, possibly forgot to actualize the database. Sorry and thanks. Take a look in a few minutes, as now we are rejudging all the submissions. I hope yours will get a 'Solved' verdict.
    
     Miguel
  • ์ด์   Solved๋ผ๊ณ  ๋œจ๋„ค์š”

์†Œ์Šค ์ฝ”๋“œ

    #include <iostream>
    
    using namespace std;
    
    struct Number
    {
    	int numerator;
    	int denominator;
    
    	double GetRatio()
    	{
    		if( denominator == 0 ) return -1;
    		return ((double)numerator)/denominator;
    	}
    
    	bool operator!=(Number& rhs)
    	{
    		return (rhs.numerator!=numerator)||(rhs.denominator!=denominator);
    	}
    
    	Number operator+(Number& rhs)
    	{
    		Number Temp = *this;
    		Temp.numerator += rhs.numerator;
    		Temp.denominator += rhs.denominator;
    
    		return Temp;
    	}
    };
    
    int main()
    {
    	int n, d;
    
    	while( cin >> n >> d )
    	{
    		if( n == 1 && d == 1 ) break;
    
    		Number Input = {n, d}, Now = {1, 1};
    		Number LeftNumber = {0, 1}, RightNumber = {1, 0};
    
    		while( Now != Input )
    		{
    			if(Input.GetRatio() > Now.GetRatio())
    			{
    				cout << "R";
    				LeftNumber = Now;
    				Now = Now+RightNumber;
    			}
    			else
    			{
    				cout << "L";
    				RightNumber = Now;
    				Now = Now+LeftNumber;
    			}
    		}
    
    		cout << endl;		
    	}
    }

CynicJJ

  • ํŠธ๋ฆฌ๋ผ๋Š” ๊ฐœ๋…์— ๋‚š์—ฌ ํ•œ์ฐธ ํ—ค๋งธ์Œ
    • Mastojun๋‹˜ ์ฝ”๋“œ๋ณด๊ณ  ์™„์ „ OTL
  • ์ž…์ถœ๋ ฅ ์ฒ˜๋ฆฌ๋Š” ๋Œ€์ถฉ ํ–ˆ์Œ. ์ข…๋ฃŒ ์กฐ๊ฑด ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Œ.
  • ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋Š” ๋„ˆ๋ฌด ๊ธธ์–ด์„œ ๋บ์Œ

!SternBrocotTree.vb

    ' $Id: SternBrocotTree.vb 111 2008-02-26 01:37:22Z ์ •ํฌ์ข… $
    
    Public Class SternBrocotTree
    
    	Private _root As Node
    	ReadOnly Property Root() As Node
    		Get
    			If _root Is Nothing Then
    				_root = TreeMaker.CreateRoot()
    			End If
    			Return _root
    		End Get
    	End Property
    
    	Function GetPath(ByVal aRational As Rational) As String
    		Dim path As String = ""
    
    		Dim aNode As Node = Root
    		While aRational <> aNode.Rational
    			If aRational < aNode.Rational Then
    				path += "L"
    				aNode = aNode.Left
    			ElseIf aRational > aNode.Rational Then
    				path += "R"
    				aNode = aNode.Right
    			End If
    		End While
    
    		Return path
    	End Function
    
    End Class

Rational.vb

    ' $Id: Rational.vb 109 2008-02-26 01:16:04Z ์ •ํฌ์ข… $
    
    Public Class Rational
    
    	Sub New(ByVal numerator As Integer, ByVal denominator As Integer)
    		_numerator = numerator
    		_denominator = denominator
    	End Sub
    
    	Sub New(ByVal leftVal As Rational, ByVal rightVal As Rational)
    		_numerator = leftVal.Numerator + rightVal.Numerator
    		_denominator = leftVal.Denominator + rightVal.Denominator
    	End Sub
    
    	Private _numerator As Integer
    	ReadOnly Property Numerator() As Integer
    		Get
    			Return _numerator
    		End Get
    	End Property
    
    	Private _denominator As Integer
    	ReadOnly Property Denominator() As Integer
    		Get
    			Return _denominator
    		End Get
    	End Property
    
    	Shared Operator <(ByVal leftVal As Rational, ByVal rightVal As Rational) As Boolean
    		If leftVal = rightVal Then
    			Return False
    		Else
    			Return Not (leftVal > rightVal)
    		End If
    	End Operator
    
    	Shared Operator >(ByVal leftVal As Rational, ByVal rightVal As Rational) As Boolean
    		Return (leftVal.Numerator * rightVal.Denominator) > (rightVal.Numerator * leftVal.Denominator)
    	End Operator
    
    	Shared Operator =(ByVal leftVal As Rational, ByVal rightVal As Rational) As Boolean
    		Return (leftVal.Numerator * rightVal.Denominator) = (rightVal.Numerator * leftVal.Denominator)
    	End Operator
    
    	Shared Operator <>(ByVal leftVal As Rational, ByVal rightVal As Rational) As Boolean
    		Return Not (leftVal = rightVal)
    	End Operator
    
    	Overrides Function Equals(ByVal compared As Object) As Boolean
    		Return Me = compared
    	End Function
    
    End Class

Node.vb

    ' $Id: Node.vb 113 2008-02-26 02:06:51Z ์ •ํฌ์ข… $
    
    Public Class Node
    
    	Private _left As Node
    	ReadOnly Property Left() As Node
    		Get
    			If _left Is Nothing Then
    				_left = TreeMaker.CreateLeftNode(Me)
    			End If
    			Return _left
    		End Get
    	End Property
    
    	Private _right As Node
    	ReadOnly Property Right() As Node
    		Get
    			If _right Is Nothing Then
    				_right = TreeMaker.CreateRightNode(Me)
    			End If
    			Return _right
    		End Get
    	End Property
    
    	Private _rational As Rational
    	Property Rational() As Rational
    		Get
    			Return _rational
    		End Get
    		Set(ByVal value As Rational)
    			_rational = value
    		End Set
    	End Property
    
    End Class

!TreeMaker.vb

    ' $Id: TreeMaker.vb 113 2008-02-26 02:06:51Z ์ •ํฌ์ข… $
    
    Module TreeMaker
    
    	Private _rationals As List(Of Rational)
    
    	Sub InitRationals()
    		_rationals = New List(Of Rational)
    		_rationals.Add(New Rational(0, 1))
    		_rationals.Add(New Rational(1, 1))
    		_rationals.Add(New Rational(1, 0))
    	End Sub
    
    	Function CreateRoot() As Node
    		InitRationals()
    
    		Dim root As Node = New Node
    		root.Rational = _rationals(1)
    
    		Return root
    	End Function
    
    	Function CreateLeftRational(ByVal parentRational As Rational) As Rational
    		Dim matchIndex As Integer = _rationals.IndexOf(parentRational)
    
    		Dim left As Rational = _rationals(matchIndex - 1)
    
    		Dim leftRational As Rational = New Rational(left, parentRational)
    		_rationals.Insert(matchIndex, leftRational)
    
    		Return leftRational
    	End Function
    
    	Function CreateRightRational(ByVal parentRational As Rational) As Rational
    		Dim matchIndex As Integer = _rationals.IndexOf(parentRational)
    
    		Dim right As Rational = _rationals(matchIndex + 1)
    
    		Dim rightRational As Rational = New Rational(parentRational, right)
    		_rationals.Insert(matchIndex + 1, rightRational)
    
    		Return rightRational
    	End Function
    
    	Function CreateLeftNode(ByVal parentNode As Node) As Node
    		Dim left As Node = New Node
    		left.Rational = CreateLeftRational(parentNode.Rational)
    
    		Return left
    	End Function
    
    	Function CreateRightNode(ByVal parentNode As Node) As Node
    		Dim right As Node = New Node
    		right.Rational = CreateRightRational(parentNode.Rational)
    
    		Return right
    	End Function
    End Module

!ModuleMain.vb

    Module ModuleMain
    
    	Sub Main()
    		While True
    			Dim input As String = Console.ReadLine()
    			Dim tree As SternBrocotTree = New SternBrocotTree
    			Dim rational As Rational = GetRationalFromInput(input)
    			Console.WriteLine(tree.GetPath(rational))
    		End While
    	End Sub
    
    	Function GetRationalFromInput(ByVal input As String) As Rational
    		Dim separator() As Char = {" "c}
    		Dim strs() As String = input.Split(separator, StringSplitOptions.RemoveEmptyEntries)
    
    		Dim numerator As Integer = Integer.Parse(strs(0))
    		Dim denominator As Integer = Integer.Parse(strs(1))
    
    		Return New Rational(numerator, denominator)
    	End Function
    
    End Module

kukuman

  • ํ’€์ด

    • ๋ถ„์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” FraExpress ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•จ
    • ๋ถ„์ˆ˜ ๊ฐ„์˜ ํฌ๊ธฐ ๋น„๊ต๋Š” ๋ถ„์ž ๋ถ„๋ชจ ํ•ญ์ด๋™์„ ํ†ตํ•ด ๋น„๊ตํ•˜์˜€๋‹ค. ์†Œ์ˆซ์  ๋น„๊ต๋ฅผ ํ”ผํ•จ
      • a/b ์™€ x/y์˜ ๋น„๊ต๋Š” ay ์™€ bx์˜ ๊ฐ’์˜ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๊ตฌํ–ˆ๋‹ค
    • ์‹ค์ œ ํŠธ๋ฆฌ ์ˆœํšŒ๋Š” 1/1๋กœ๋ถ€ํ„ฐ ํ˜„์žฌ ๋…ธ๋“œ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ์˜ค๋ฅธ์ชฝ ์ž‘์œผ๋ฉด ์™ผ์ชฝ์œผ๋กœ ์ด๋™ ๋˜๊ฒŒ ๊ตฌํ˜„ ํ•˜์˜€๋‹ค
      • ์ด๋™ ์ž‘์—…์€ ํ˜„์žฌ ๋…ธ๋“œ,์™ผ์ชฝ ๋…ธ๋“œ,์˜ค๋ฅธ์ชฝ ๋…ธ๋“œ์˜ ๋ณ€๊ฒฝ์œผ๋กœ ๊ตฌํ˜„
  • ์†Œ์Šค

      import java.io.IOException;
      
      public class Main implements Runnable {
      	static String ReadLn(int maxLength) { // utility function to read from
      											// stdin,
      		// Provided by Programming-challenges, edit for style only
      		byte line[] = new byte[maxLength];
      		int length = 0;
      		int input = -1;
      		try {
      			while (length < maxLength) {// Read untill maxlength
      				input = System.in.read();
      				if ((input < 0) || (input == '\n'))
      					break; // or untill end of line ninput
      				line[length++] += input;
      			}
      
      			if ((input < 0) && (length == 0))
      				return null; // eof
      			return new String(line, 0, length);
      		} catch (IOException e) {
      			return null;
      		}
      	}
      
      	public static void main(String[] args) {
      		Main main = new Main();
      		main.run();
      	}
      	public void run() {
      		while(true){
      			String line = ReadLn(255).trim();
      			if(line == null || line.equals("1 1")){
      				break;
      			}
      			String [] strSplit = line.split(" ");
      			if(strSplit.length!=2){
      				break;
      			}
      			String result = SBTreeTravelResult(new FraExpress( Integer.parseInt(strSplit[0]), Integer.parseInt(strSplit[1])));
      			System.out.println(result);
      		}
      	}
      
      	public String SBTreeTravelResult(FraExpress searchVal) {
      		FraExpress curNode = new FraExpress(1, 1);
      		FraExpress leftNode = new FraExpress(0, 1);
      		FraExpress rightNode = new FraExpress(1, 0);
      		StringBuilder builder = new StringBuilder();
      		while (curNode.compareTo(searchVal) != 0) {
      			int compareResult = searchVal.compareTo(curNode);
      			if (compareResult > 0) {
      				// ์ฐพ์„ ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์„œ์นญ
      				builder.append("R");
      				leftNode = curNode;
      				curNode = curNode.add(rightNode);
      			} else if (compareResult < 0) {
      				// ์ฐพ์„ ๊ฐ’์ด ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์™ผ์ชฝ์œผ๋กœ ์„œ์นญ
      				builder.append("L");
      				rightNode = curNode;// ํ˜„์žฌ ๋…ธ๋“œ๋Š” ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™๋จ
      				curNode = curNode.add(leftNode);
      			} else {
      				// ํ˜„์ œ๋…ธ๋“œ๊ฐ’์ด ์ฐพ๋Š” ๊ฐ’์ด๋ฉด ๊ฒ€์ƒ‰ ์ข…๋ฃŒ
      				break;
      			}
      		}
      		return builder.toString();
      	}
      
      }
      
      class FraExpress implements Comparable<FraExpress> {
      	private int denominator; // ๋ถ„์ž
      
      	private int numerator; // ๋ถ„๋ชจ
      
      	public FraExpress(int denominator, int numerator) {
      		this.denominator = denominator;
      		this.numerator = numerator;
      	}
      
      	public int compareTo(FraExpress o) {
      		/**
      		 * x a -- ์™€ -- ์˜ ๋น„๊ต๋Š” xb ์™€ ay์˜ ๋น„๊ต์™€ ๊ฐ™๋‹ค y b
      		 */
      		int left = this.denominator * o.numerator;
      		int right = this.numerator * o.denominator;
      		if (left < right)
      			return -1;
      		else if (left > right)
      			return 1;
      		else
      			return 0;
      	}
      
      	public FraExpress add(FraExpress addend) {
      		return new FraExpress(this.denominator + addend.denominator,
      				this.numerator + addend.numerator);
      	}
      }
    
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค

      import junit.framework.TestCase;
      
      
      public class SternBrocot extends TestCase {
      	public void testFraExpressCompare(){
      		FraExpress a = new FraExpress(1,2);
      		FraExpress b = new FraExpress(1,1);
      		FraExpress c = new FraExpress(2,1);
      		assertEquals(0,a.compareTo(a));
      		assertEquals(-1,a.compareTo(b)); // a<b
      		assertEquals(1,b.compareTo(a)); // b<a
      		assertEquals(-1,a.compareTo(c)); // a<c
      	}
      	
      	public void testSBTreeTravelResult(){
      		Main main = new Main();
      		assertEquals("LRRL", main.SBTreeTravelResult(new FraExpress(5,7)));
      		assertEquals("RRLRRLRLLLLRLRRR", main.SBTreeTravelResult(new FraExpress(878 ,323)));
      	}
      }
    

Outbreak

๋ฌธ์ œ ์š”์•ฝ

  • ์Šคํ„ด-๋ธŒ๋กœ์ฝง ์ˆซ์ž ์‹œ์Šคํ…œ์ด Binary Search Tree ์ธ ๊ฒƒ์„ ์•Œ์•„์ฑ„๋Š” ๋ฌธ์ œ

๋ฌธ์ œ ํ•ด๊ฒฐ

  • Left(m/n), Current(m+M/n+N), Right(M/N) ์„ ์žฌ๊ท€๋กœ ๋Œ๋ ธ๋‹ค.

    • ์ž…๋ ฅ๊ฐ’์˜ ํฌ๊ธฐ๊ฐ€ ์ƒ๋‹นํžˆ(?) ํด ๋•Œ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ•˜๋ฉด ์Šคํƒ ์˜ค๋ฒ„ ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
      • Solver2๋ฅผ C++๋กœ ํฌํŒ…ํ•˜๊ณ  ํ…Œ์ŠคํŠธ ํ•ด๋ณธ๊ฒฐ๊ณผ, Debug๋ชจ๋“œ๋กœ ๋Œ๋ฆฌ๋ฉด StackOverflow, Relase๋ชจ๋“œ๋กœ ๋Œ๋ฆฌ๋ฉด ์ œ๋Œ€๋กœ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ผฌ๋ฆฌ์žฌ๊ท€๊ฐ€ ๋™์ž‘ํ•˜๋Š”๋“ฏ ํ•œ๋ฐ.. C#์€ Debug๋กœํ•˜๋“ , Release๋กœ ํ•˜๋“  ๋ป—์–ด๋ฒ„๋ฆฌ๋Š”๊ตฐ์š” (.NET 2005๋กœ ํ…Œ์ŠคํŠธํ–ˆ์–ด์š”)

      • C# ์—๋Š” Tail Recursion ์ด ๊ตฌํ˜„ ์•ˆ๋˜์–ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

      • ๊ทธ๋Ÿฌ๊ณ  ๋ณด๋‹ˆ .NET ๊ฐ€์ƒ ๋จธ์‹  ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ ๋‚ด์„œ ์ฃฝ์ธ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ ์ด ๋ฐฉ๋ฒ•์„ ์ผ์—ˆ๋„ค์š”

      • http://blogs.msdn.com/abhinaba/archive/2007/07/27/tail-recursion-on-net.aspx

      • ์œ„ ์ž๋ฃŒ๋ฅผ ์š”์•ฝํ•˜๋ฉด ".NET ์—๋Š” Tail-Recursion IL ๋ช…๋ น์ด ์žˆ์ง€๋งŒ, C#์—์„œ๋Š” ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค" ๋‚ญํŒจ ^^;

        int TestFun() { return TestFun(); }

  • ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋กœ๋„ ํ…Œ์ŠคํŠธ ํ•ด๋ดค๋Š”๋ฐ ๋งˆ์ฐฌ๊ฐ€์ง€์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค. Debug๋ชจ๋“œ๋Š” ๊ผฌ๋ฆฌ์žฌ๊ท€๊ฐ€ ์•ˆ๋˜๋Š”๊ฑด์ง€.... ์•„๋‹ˆ๋ฉด C++ Release๋ชจ๋“œ๋Š” ์Šคํƒ์šฉ๋Ÿ‰์ด ์žฌํ•œ์—†๋Š”๊ฑด์ง€....;;;

  • ์Šคํƒ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ์œผ๋‚˜..

  • ๊ฒฐ๊ตญ Iteration ๋ฒ„์ „๋„ ๋งŒ๋“ค์—ˆ๋‹ค ^^

๊ธฐํƒ€

  • scheme ์œผ๋กœ ํ•˜๊ธฐ ๋”ฑ ์ข‹์€ ๋ฌธ์ œ๋ผ๋Š” ์ƒ๊ฐ.. ๋งŒ ^^;
  • Tail Recursion ์„ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ถœ๋ ฅ ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€

์ฝ”๋“œ 1 - Recursion

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Scort
    {   
        class Solver
        {
            int m;
            int n;
    
            public Solver(int m, int n)
            {
                this.m = m;
                this.n = n;
            }
    
            // invariant : the fraction must be existed between 0/1 and 1/0        
            public void Traverse(int lM, int lN, int rM, int rN)
            {
                // l(left)  c(center)   r(Right)
                int cM = lM + rM;
                int cN = lN + rN;
    
                // c์˜ ์™ผ์ชฝ
                if (cM * n > m * cN) 
                {
                    Console.Write("L");
                    Traverse(lM, lN, cM, cN);
                }
                // c์˜ ์˜ค๋ฅธ์ชฝ
                else if (cM * n < m * cN) 
                {
                    Console.Write("R");
                    Traverse(cM, cN, rM, rN);
                }            
            }
    
            public void Run()
            {
                if (m > 0 && n > 0)
                {                
                    Traverse(0, 1, 1, 0);
                    Console.WriteLine();
                }          
            }
        }
    }

์ฝ”๋“œ 2 - Recursion

    namespace Scort
    {
        class Solver2
        {
            int m, n;
    
            int lM, lN, rM, rN;
            int cM, cN;
    
            public Solver2(int m, int n)
            {
                this.m = m;
                this.n = n;
    
                lM = 0; lN = 1;
                rM = 1; rN = 0;
            }
    
    
            public void Traverse()
            {
                cM = lM + rM;
                cN = lN + rN;
    
                if (cM * n > m * cN)
                {
                    Console.Write("L");
                    rM = cM;
                    rN = cN;
                    Traverse();
                }
                else if (cM * n < m * cN)
                {
                    Console.Write("R");
                    lM = cM;
                    lN = cN;
                    Traverse();
                }            
            }
    
            public void Run()
            {
                if (m > 0 && n > 0)
                {
                    Traverse();
                    Console.WriteLine();
                }
            }
        }
    } 

์ฝ”๋“œ 3 - Iteration

    namespace Scort
    {
        class Solver3
        {
            int m, n;
    
            int lM, lN, rM, rN;
            int cM, cN;
    
            public Solver3(int m, int n)
            {
                this.m = m;
                this.n = n;
    
                lM = 0; lN = 1;
                rM = 1; rN = 0;
            }
    
            public bool Traverse()
            {
                cM = lM + rM;
                cN = lN + rN;
    
                if (cM * n > m * cN)
                {
                    Console.Write("L");
                    rM = cM;
                    rN = cN;
                    return false;
                }
    
                else if (cM * n < m * cN)
                {
                    Console.Write("R");
                    lM = cM;
                    lN = cN;
                    return false;
                }
    
                return true;
            }
    
            public void Run()
            {
                if (m > 0 && n > 0)
                {
                    while(true)
                    {
                        if( Traverse() == true )
                        {
                            break;
                        }
                    }
    
                    Console.WriteLine();
                }
            }
        }
    }

์œ ๋‹›ํ…Œ์ŠคํŠธ

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Scort
    {     
        class Program
        {
            static void Main(string[] args)
            {
                // ๋ˆˆ์œผ๋กœ ์œ ๋‹›ํ…Œ์ŠคํŠธ.. ํ›„ํ›„
    
                // Recursion
                Solver solver = new Solver(878,323);
                solver.Run();
    
                // ์Šคํƒ ๋œ ์“ฐ๋Š” Recursion
                Solver2 solver2 = new Solver2(878, 323);
                solver2.Run();
    
                // Iteration
                Solver3 solver3 = new Solver3(878, 323);
                solver3.Run();
    
                solver3 = new Solver3(2007777777, 3);
                solver3.Run();
            }
        }
    }

์Šคํ„ฐ๋””์—์„œ์ œ์ž‘

  • ํ•œ๊ธ€๋กœ ์ฝ”๋”ฉ ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. -0-

    • ์žฌ๊ท€๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ
  • ์ฝ”๋“œ

      public class Main {
      	private StringBuilder result ;
      	public String SBTreeTravelResult(๋ถ„์ˆ˜ ์ฐพ์„๊ฐ’) {
      		๋ถ„์ˆ˜ ์˜ค๋ฅธ์ชฝ = new ๋ถ„์ˆ˜(1,0);
      		๋ถ„์ˆ˜ ์™ผ์ชฝ = new ๋ถ„์ˆ˜(0,1);
      		result = new StringBuilder();
      		SBTreeTravelResult(์ฐพ์„๊ฐ’, ์˜ค๋ฅธ์ชฝ,์™ผ์ชฝ);
      		return result.toString();
      	}
      
      	private void SBTreeTravelResult(๋ถ„์ˆ˜ ์ฐพ์„๊ฐ’, ๋ถ„์ˆ˜ ์˜ค๋ฅธ์ชฝ, ๋ถ„์ˆ˜ ์™ผ์ชฝ) {
      		๋ถ„์ˆ˜ ํ˜„์žฌ๋…ธ๋“œ = ์˜ค๋ฅธ์ชฝ.add(์™ผ์ชฝ);
      		if(์ฐพ์„๊ฐ’.compareTo(ํ˜„์žฌ๋…ธ๋“œ) ==0){
      			return ;
      		}
      
      		if(์ฐพ์„๊ฐ’.compareTo(ํ˜„์žฌ๋…ธ๋“œ) >0){
      			์™ผ์ชฝ = ํ˜„์žฌ๋…ธ๋“œ;
      			result.append("R");
      		}else if(์ฐพ์„๊ฐ’.compareTo(ํ˜„์žฌ๋…ธ๋“œ) <0){
      			์˜ค๋ฅธ์ชฝ = ํ˜„์žฌ๋…ธ๋“œ;
      			result.append("L");
      		}
      		SBTreeTravelResult(์ฐพ์„๊ฐ’,  ์˜ค๋ฅธ์ชฝ, ์™ผ์ชฝ);
      	}
      }
    
  • ๋ถ„์ˆ˜

      public class ๋ถ„์ˆ˜ implements Comparable<๋ถ„์ˆ˜>{
      	private int ๋ถ„๋ชจ;
      	private int ๋ถ„์ž;
      	public ๋ถ„์ˆ˜(int ๋ถ„์ž, int ๋ถ„๋ชจ) {
      		this.๋ถ„์ž = ๋ถ„์ž;
      		this.๋ถ„๋ชจ = ๋ถ„๋ชจ;
      	}
      	public int compareTo(๋ถ„์ˆ˜ ๋น„๊ต๋Œ€์ƒ) {
      		long ๊ฐ€= ๋น„๊ต๋Œ€์ƒ.๋ถ„๋ชจ * this.๋ถ„์ž;
      		long ๋‚˜ = this.๋ถ„๋ชจ * ๋น„๊ต๋Œ€์ƒ.๋ถ„์ž;
      		if(๊ฐ€ == ๋‚˜){
      			return 0;
      		}else if(๊ฐ€ > ๋‚˜ ){
      			return 1;
      		}else{
      			return -1;
      		}
      			
      		
      	}
      	public ๋ถ„์ˆ˜ add(๋ถ„์ˆ˜ ๋ง์ˆ˜) {
      		int ๋ถ„์ž = this.๋ถ„์ž + ๋ง์ˆ˜.๋ถ„์ž;
      		int ๋ถ„๋ชจ = this.๋ถ„๋ชจ + ๋ง์ˆ˜.๋ถ„๋ชจ;
      		return new ๋ถ„์ˆ˜(๋ถ„์ž,๋ถ„๋ชจ);
      	}
      
      }
    
  • ํ…Œ์ŠคํŠธ

      import junit.framework.TestCase;
      
      
      public class MainTestCase extends TestCase {
      	public void test๋ถ„์ˆ˜๋น„๊ตํ•˜๊ธฐ(){
      		๋ถ„์ˆ˜ bun1 = new ๋ถ„์ˆ˜(3,4);
      		๋ถ„์ˆ˜ bun2 = new ๋ถ„์ˆ˜(3,4);
      		๋ถ„์ˆ˜ bun3 = new ๋ถ„์ˆ˜(1,2);
      		๋ถ„์ˆ˜ bun4 = new ๋ถ„์ˆ˜(1,1);
      		assertEquals(0, bun1.compareTo(bun2));
      		assertEquals(1, bun1.compareTo(bun3));
      		assertEquals(-1, bun3.compareTo(bun4));
      		
      	}
      	
      	public void test๊ฒฐ๊ณผ๊ณ„์‚ฐ(){
      		Main main = new Main();
      		assertEquals("LRRL", main.SBTreeTravelResult(new ๋ถ„์ˆ˜(5,7)));
      		assertEquals("RRLRRLRLLLLRLRRR", main.SBTreeTravelResult(new ๋ถ„์ˆ˜(878 ,323)));
      	}
      }
    
โš ๏ธ **GitHub.com Fallback** โš ๏ธ