Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. //489C : GIVEN LENGTH AND SUM OF DIGITS...
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9. import java.util.StringTokenizer;
  10.  
  11. //Your code goes here
  12. public class Main {
  13.  
  14. static class ProblemSolver{
  15. public void solveTheProblem(InputReader in,PrintWriter out){
  16. //Declare the necessary variables and take all the inputs
  17. int m=in.nextInt();
  18. int s=in.nextInt();
  19. int[] max;
  20. int[] min;
  21.  
  22. //Evaluation of solution and display of the result
  23. /*
  24. * The most basic and the trickiest base case.
  25. * In the question it is mentioned `no leading zeroes`,
  26. * this is the outcome of that expression. *
  27. */
  28. if(m==1 && s==0)System.out.println("0 0");
  29. /*
  30. * It involves simple mathematics as shown in the
  31. * if condition but it is to be noted that one of
  32. * its cases is an exception which is covered above
  33. */
  34. else if(s>(9*m) || s<1)System.out.println("-1 -1");
  35. /*
  36. * This is the real evaluation stage. Here we need
  37. * to know that to maximize a number it should have the
  38. * maximum value at the beginning and vice versa.
  39. * The question is approached in an exact manner but
  40. * taking care of an expression in which we need to take
  41. * care that the condition of m numbers must be fulfilled.
  42. * Ex: m=3, s=17
  43. * max=980 but min can't be 089 instead
  44. * min=179 or (1(8-1)9)
  45. */
  46. else{
  47. max=new int[m];
  48. min=new int[m];
  49. for(int i=0;i<m;++i){
  50. max[i]=0;
  51. min[i]=0;
  52. }
  53. int i=0;
  54. while(s>0){
  55. if(s>9){
  56. max[i]=9;
  57. min[m-1-i]=9;
  58. s-=9;
  59. }else{
  60. max[i]=s;
  61. min[m-1-i]=s-1;
  62. min[0]+=1;
  63. break;
  64. }
  65. ++i;
  66. }
  67. for(int j=0;j<m;++j)System.out.print(min[j]);
  68. System.out.print(" ");
  69. for(int j=0;j<m;++j)System.out.print(max[j]);
  70. }
  71.  
  72. }
  73. }
  74.  
  75. //Default template for all the codes
  76. static class InputReader {
  77. public BufferedReader reader;
  78. public StringTokenizer tokenizer;
  79.  
  80. public InputReader(InputStream stream) {
  81. reader = new BufferedReader(new InputStreamReader(stream),32768);
  82. tokenizer = null;
  83. }
  84.  
  85. public String next() {
  86. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  87. try {
  88. tokenizer = new StringTokenizer(reader.readLine());
  89. } catch (IOException e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. return tokenizer.nextToken();
  94. }
  95.  
  96. public int nextInt() {
  97. return Integer.parseInt(next());
  98. }
  99.  
  100. }
  101.  
  102. //Main method for all the codes
  103. public static void main(String[] args) {
  104. InputStream inputStream = System.in;
  105. OutputStream outputStream = System.out;
  106. InputReader in = new InputReader(inputStream);
  107. PrintWriter out = new PrintWriter(outputStream);
  108. ProblemSolver problemSolver = new ProblemSolver();
  109. problemSolver.solveTheProblem(in, out);
  110. out.close();
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement