fork download
  1. /*
  2. You should use the standard input/output
  3.  
  4. in order to receive a score properly.
  5.  
  6. Do not use file input and output
  7.  
  8. Please be very careful.
  9. */
  10.  
  11. import java.util.Scanner;
  12.  
  13. /*
  14.   As the name of the class should be Solution , using Solution.java as the filename is recommended.
  15.   In any case, you can execute your program by running 'java Solution' command.
  16.  */
  17. class Solution {
  18. static int Answer;
  19.  
  20. public static void main(String args[]) throws Exception {
  21. /*
  22. The method below means that the program will read from input.txt, instead of standard(keyboard) input.
  23. To test your program, you may save input data in input.txt file,
  24. and call below method to read from the file when using nextInt() method.
  25. You may remove the comment symbols(//) in the below statement and use it.
  26. But before submission, you must remove the freopen function or rewrite comment symbols(//).
  27. */
  28.  
  29. /*
  30. Make new scanner from standard input System.in, and read data.
  31. */
  32. Scanner sc = new Scanner(System.in);
  33. //Scanner sc = new Scanner(new FileInputStream("input.txt"));
  34.  
  35. int T = sc.nextInt();
  36. for(int test_case = 0; test_case < T; test_case++) {
  37.  
  38. // Answer = 0;
  39. /////////////////////////////////////////////////////////////////////////////////////////////
  40. /*
  41. Implement your algorithm here.
  42. The answer to the case will be stored in variable Answer.
  43. */
  44. /////////////////////////////////////////////////////////////////////////////////////////////
  45. int num=sc.nextInt();
  46.  
  47. int n=num;
  48. int flag=num;
  49. int[] arr=new int[10];
  50. int count=0;
  51. while((n=doWork(n))!=-1){
  52. if(num==n)
  53. {
  54. arr[count++]=num;
  55. break;
  56. }
  57. arr[count++]=n;
  58. n=flag-n;
  59. flag=n;
  60. if(count>3) break;
  61. }
  62.  
  63. String s="";
  64. for (int i = 0; i < count; i++) {
  65. s+=arr[i]+" ";
  66. }
  67.  
  68. // Print the answer to standard output(screen).
  69. System.out.println("Case #"+(test_case+1));
  70. System.out.println((count--)+" "+s);
  71. }
  72. }
  73.  
  74. public static int doWork(int num) {
  75. int tmp=num;
  76. while (true) {
  77. if(tmp<=0)
  78. return -1;
  79. if(!istPalindrom(String.valueOf(tmp).toCharArray())){
  80. tmp--;
  81. }
  82. else
  83. return tmp;
  84. }
  85. }
  86.  
  87. public static boolean istPalindrom(char[] word){
  88. int i1 = 0;
  89. int i2 = word.length - 1;
  90. while (i2 > i1) {
  91. if (word[i1] != word[i2]) {
  92. return false;
  93. }
  94. ++i1;
  95. --i2;
  96. }
  97. return true;
  98. }
  99.  
  100. }
Success #stdin #stdout 0.08s 29400KB
stdin
2
6
124
stdout
Case #1
1 6 
Case #2
2 121 3