fork(2) download
  1. using System;
  2. using System.Text;
  3.  
  4. namespace CSharpPlayground
  5. {
  6. public class Program
  7. {
  8. static void Main()
  9. {
  10. string[] txt = Console.ReadLine().Split(' ');
  11. string numberN = txt[0];
  12. string numberK = txt[1];
  13.  
  14. StringBuilder sb = new StringBuilder();
  15.  
  16. int k = Int32.Parse(numberK);
  17. ulong n = ulong.Parse(numberN);
  18.  
  19. int[] digits = new int[numberN.Length];
  20. for (int i = 0; i < numberN.Length; i++)
  21. digits[i] = Int32.Parse(numberN[i].ToString());
  22.  
  23.  
  24. if (numberN.Length < k)
  25. {
  26. sb.Clear();
  27. for (int i = 0; i < k; i++)
  28. sb.Append(5);
  29. }
  30. else if (CountFivesOnUlong(n + 1) >= k)
  31. {
  32. sb.Append(n + 1);
  33. }
  34. else if (k == 1)
  35. {
  36. while (true)
  37. {
  38. n = n + 1;
  39. if (CountFivesOnUlong(n) >= 1)
  40. {
  41. sb.Append(n);
  42. break;
  43. }
  44. }
  45. }
  46. else
  47. {
  48. digits = AddZeroAtFirst(digits);
  49.  
  50. for (int i = digits.Length - 1; i > 0; i--)
  51. {
  52. if (Check(digits, n, k))
  53. break;
  54.  
  55. if (digits[i] < 5)
  56. {
  57. digits[i] = 5;
  58. if (Check(digits, n, k))
  59. {
  60. sb.Append(ConvertToUlong(digits));
  61. break;
  62. }
  63. }
  64. else if (digits[i] >= 5)
  65. {
  66. if (digits[i - 1] == 9)
  67. {
  68. digits[i] = 5;
  69. if (Check(digits, n, k))
  70. {
  71. sb.Append(ConvertToUlong(digits));
  72. break;
  73. }
  74.  
  75. for (int j = i - 1; j >= 0; j--)
  76. {
  77. if (digits[j] == 9)
  78. digits[j] = 0;
  79. else
  80. {
  81.  
  82. digits[j]++;
  83. break;
  84. }
  85. }
  86.  
  87. if (Check(digits, n, k) && CountFives(digits) == k)
  88. {
  89. sb.Append(ConvertToUlong(digits));
  90. break;
  91. }
  92. }
  93. else
  94. {
  95. digits[i - 1]++;
  96. digits[i] = 5;
  97.  
  98. if (Check(digits, n, k))
  99. {
  100. sb.Append(ConvertToUlong(digits));
  101. break;
  102. }
  103. }
  104. }
  105. }
  106.  
  107. if(CountFives(digits) > k)
  108. {
  109. for(int i = 1; i < digits.Length - 1; i++)
  110. {
  111. if(digits[i] == 5)
  112. {
  113. digits[i] = 0;
  114. if (Check(digits, n, k))
  115. {
  116. sb.Clear();
  117. sb.Append(ConvertToUlong(digits));
  118. }
  119. else
  120. digits[i] = 5;
  121. }
  122. }
  123. }
  124. }
  125.  
  126. Console.WriteLine(sb);
  127. }
  128.  
  129. private static int CountFives(int[] digits)
  130. {
  131. int sum = 0;
  132. for (int i = 0; i < digits.Length; i++)
  133. if (digits[i] == 5)
  134. sum++;
  135.  
  136. return sum;
  137. }
  138.  
  139. private static bool Check(int[] digits, ulong n, int k)
  140. {
  141. if (CountFives(digits) >= k && ConvertToUlong(digits) > n)
  142. return true;
  143. else
  144. return false;
  145. }
  146.  
  147. private static ulong ConvertToUlong(int[] digits)
  148. {
  149. string tmp = "";
  150. for (int i = 0; i < digits.Length; i++)
  151. {
  152. tmp += digits[i];
  153. }
  154.  
  155. return ulong.Parse(tmp);
  156. }
  157.  
  158. private static int[] AddZeroAtFirst(int[] digits)
  159. {
  160. int[] tmp = new int[digits.Length + 1];
  161. tmp[0] = 0;
  162. for (int i = 1; i < tmp.Length; i++)
  163. tmp[i] = digits[i - 1];
  164.  
  165. return tmp;
  166. }
  167.  
  168. private static int CountFivesOnUlong(ulong n)
  169. {
  170. int counter = 0;
  171. ulong number = n;
  172. while (number != 0)
  173. {
  174. if (number % 10 == 5)
  175. counter++;
  176.  
  177. number = number / 10;
  178. }
  179.  
  180. return counter;
  181. }
  182. }
  183. }
Success #stdin #stdout 0s 131520KB
stdin
495495995 5
stdout
495500555