fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. bool check(const vector<int> a)
  5. {
  6. for (int i = 0, j = a.size() - 1; i < j; i++, j--)
  7. if (a[i] != a[j])
  8. return false;
  9. return true;
  10. }
  11. int main(int argc, char** argv)
  12. {
  13. setbuf(stdout, NULL);
  14. vector <int> e;
  15. for (int u = 0; u <= 10000; u++)
  16. {
  17. if (u < 10)
  18. {
  19. e.push_back(u);
  20. continue;
  21. }
  22. vector<int> a;
  23. int k = u;
  24. while (k)
  25. {
  26. a.push_back(k % 10);
  27. k = k / 10;
  28. }
  29. if (check(a))
  30. e.push_back(u);
  31.  
  32. }
  33. int T, test_case;
  34. /*
  35. The freopen function below opens input.txt file in read only mode, and afterward,
  36. the program will read from input.txt file instead of standard(keyboard) input.
  37. To test your program, you may save input data in input.txt file,
  38. and use freopen function to read from the file when using cin function.
  39. You may remove the comment symbols(//) in the below statement and use it.
  40. Use #include<cstdio> or #include <stdio.h> to use the function in your program.
  41. But before submission, you must remove the freopen function or rewrite comment symbols(//).
  42. */
  43.  
  44. // freopen("input.txt", "r", stdin);
  45.  
  46. cin >> T;
  47. for (test_case = 0; test_case < T; test_case++)
  48. {
  49. int k;
  50. cin >> k;
  51. int temp = k;
  52. k = 198;
  53. for (int i = 0; i<e.size(); i++)
  54. if (e[i] > temp)
  55. {
  56. k = i - 1;
  57. break;
  58. }
  59. int i, j;
  60. i = j = 0;
  61.  
  62.  
  63. while (i <= k)
  64. {
  65. if (e[i] + e[j] + e[k] == temp)
  66. break;
  67. while (e[i] + e[j] + e[k] < temp)
  68. {
  69. j++;
  70. if (e[i] + e[j] + e[k] == temp)
  71. break;
  72. while (e[i] + e[j] + e[k] > temp)
  73. {
  74. k--;
  75. if (e[i] + e[j] + e[k] == temp)
  76. break;
  77. if (j > k)
  78. {
  79. i++;
  80. j = i;
  81. break;
  82. }
  83. }
  84. if (j > k)
  85. {
  86. i++;
  87. j = i;
  88. break;
  89. }
  90. }
  91.  
  92. }
  93.  
  94. /////////////////////////////////////////////////////////////////////////////////////////////
  95. /*
  96. Implement your algorithm here.
  97. The answer to the case will be stored in variable Answer.
  98. */
  99. /////////////////////////////////////////////////////////////////////////////////////////////
  100.  
  101. // Print the answer to standard output(screen).
  102.  
  103. cout << "Case #" << test_case + 1 << endl;
  104. if (e[i] == 0 && e[j] == 0)
  105. cout << 1 << " " << e[k] << endl;
  106. else if (e[i] == 0)
  107. cout << 2 << " " << e[k] <<" "<<e[j]<<endl;
  108. else cout << 3 << " " << e[k] << " " << e[j] << " "<<e[i]<< endl;
  109.  
  110. }
  111.  
  112. return 0;//Your program should return 0 on normal termination.
  113. }
Success #stdin #stdout 0s 4312KB
stdin
2
4
124
stdout
Case #1
1 4
Case #2
2 121 3