-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathCSharp.cs
55 lines (49 loc) · 1.38 KB
/
CSharp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*********************************************/
/* */
/* CodinGame.com Solutions by pathosDev */
/* */
/* Puzzle: Balanced ternary computer: encode */
/* Difficulty: Easy */
/* Date solved: 09.11.2018 */
/* */
/*********************************************/
using System;
using System.Linq;
public class Solution
{
public static void Main()
{
//Read input.
int N = int.Parse(Console.ReadLine());
if (N == 0)
{
Console.WriteLine("0");
}
else
{
//Get ternary representation.
Console.WriteLine(ConvertToTernary(N));
}
}
//Generates the ternary representation of an integer.
private static string ConvertToTernary(int number)
{
if (number < 0)
{
string BT = ConvertToTernary(-number);
return string.Concat(BT.Select(c => (c == 'T') ? '1' : ((c == '1') ? 'T' : '0')));
}
if (number == 0)
{
return string.Empty;
}
if (number % 3 == 2)
{
return ConvertToTernary((number + 1) / 3) + "T";
}
else
{
return ConvertToTernary(number / 3) + (number % 3);
}
}
}