fork(43) download
  1. using System;
  2.  
  3. public class NumberTranslation
  4. {
  5. public static int GetTranslationCount(int number)
  6. {
  7. if(number <= 0)
  8. {
  9. return 0;
  10. }
  11.  
  12. string numberInString = number.ToString();
  13. return GetTranslationCount(numberInString);
  14. }
  15.  
  16. private static int GetTranslationCount(string number)
  17. {
  18. int length = number.Length;
  19. int[] counts = new int[length];
  20.  
  21. for(int i = length - 1; i >= 0; --i)
  22. {
  23. int count = 0;
  24. if(number[i] >= '1' && number[i] <= '9')
  25. {
  26. if(i < length - 1)
  27. {
  28. count += counts[i + 1];
  29. }
  30. else
  31. {
  32. count += 1;
  33. }
  34. }
  35.  
  36. if(i < length - 1)
  37. {
  38. int digit1 = number[i] - '0';
  39. int digit2 = number[i + 1] - '0';
  40. int converted = digit1 * 10 + digit2;
  41. if(converted >= 10 && converted <= 26)
  42. {
  43. if(i < length - 2)
  44. {
  45. count += counts[i + 2];
  46. }
  47. else
  48. {
  49. count += 1;
  50. }
  51. }
  52. }
  53.  
  54. counts[i] = count;
  55. }
  56.  
  57. return counts[0];
  58. }
  59.  
  60. // ------------------ Test Code ------------------
  61. private static void Test(string testName, int number, int expected)
  62. {
  63. if(GetTranslationCount(number) == expected)
  64. {
  65. Console.WriteLine(testName + " passed.");
  66. }
  67. else
  68. {
  69. Console.WriteLine(testName + " FAILED.");
  70. }
  71. }
  72.  
  73. private static void Test1()
  74. {
  75. int number = 1;
  76. int expected = 1;
  77. Test("Test1", number, expected);
  78. }
  79.  
  80. private static void Test2()
  81. {
  82. int number = 11;
  83. int expected = 2;
  84. Test("Test2", number, expected);
  85. }
  86.  
  87. private static void Test3()
  88. {
  89. int number = 126;
  90. int expected = 3;
  91. Test("Test3", number, expected);
  92. }
  93.  
  94. private static void Test4()
  95. {
  96. int number = 127;
  97. int expected = 2;
  98. Test("Test4", number, expected);
  99. }
  100.  
  101. private static void Test5()
  102. {
  103. int number = 427;
  104. int expected = 1;
  105. Test("Test5", number, expected);
  106. }
  107.  
  108. private static void Test6()
  109. {
  110. int number = 100;
  111. int expected = 0;
  112. Test("Test6", number, expected);
  113. }
  114.  
  115. private static void Test7()
  116. {
  117. int number = 101;
  118. int expected = 1;
  119. Test("Test7", number, expected);
  120. }
  121.  
  122. private static void Test8()
  123. {
  124. int number = 12258;
  125. int expected = 5;
  126. Test("Test8", number, expected);
  127. }
  128.  
  129. public static void Main()
  130. {
  131. Test1();
  132. Test2();
  133. Test3();
  134. Test4();
  135. Test5();
  136. Test6();
  137. Test7();
  138. Test8();
  139. }
  140. }
Success #stdin #stdout 0.03s 33800KB
stdin
Standard input is empty
stdout
Test1 passed.
Test2 passed.
Test3 passed.
Test4 passed.
Test5 passed.
Test6 passed.
Test7 passed.
Test8 passed.