fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX (100001)
  4.  
  5. int toInt(char c) {
  6. return c - '0';
  7. }
  8.  
  9. char toChar(int i) {
  10. return i + '0';
  11. }
  12.  
  13. char next(char c) {
  14. if (c == '9') return '0';
  15. return toChar(toInt(c) + 1);
  16. }
  17.  
  18. char prev(char c) {
  19. if (c == '0') return '9';
  20. return toChar(toInt(c) - 1);
  21. }
  22.  
  23. int main() {
  24. int i,n,j,k;
  25. char x[MAX] = {};
  26. unsigned long length;
  27. scanf("%d",&n);
  28. if (n < 0) n = 0;
  29. for(i = 0; i < n; i++) {
  30. scanf("%s",x);
  31. length = strlen(x);
  32. int left, right, place;
  33. int changed = 0;
  34. for (j = 0, k = length-1, place = 0; j < k; j++, k--, place++) {
  35. left = toInt(x[j]);
  36. right = toInt(x[k]);
  37. if (left == right) continue;
  38. if (j + 1 == k) {
  39. if (right > left) {
  40. while (right != left) {
  41. right--;
  42. }
  43. x[j] = toChar(left);
  44. x[k] = toChar(right);
  45. }
  46. if (left > right) {
  47. while (left != right) {
  48. right++;
  49. }
  50. x[k] = toChar(left);
  51. x[j] = toChar(right);
  52. }
  53. continue;
  54. }
  55. int add;
  56. changed = 1;
  57. if (left < right) {
  58. add = 10 + left - right;
  59. } else {
  60. add = left - right;
  61. }
  62. right = right + add;
  63. if (right < 10) {
  64. x[k] = toChar(right);
  65. } else {
  66. x[k] = toChar(right-10);
  67. int t = length - place - 2;
  68. int cont = 1;
  69. while (t >j && cont == 1) {
  70. x[t] = next(x[t]);
  71. if (x[t] == 0) {
  72. cont = 1;
  73. } else {
  74. cont = 0;
  75. }
  76. }
  77. }
  78. //printf("%s\n", x);
  79. }
  80. printf("%s\n", x);
  81. }
  82. return 0;
  83. }
  84.  
  85.  
Success #stdin #stdout 0s 2272KB
stdin
4
123
1345
4527
1938
stdout
131
1331
4554
1991