fork download
  1. program ideone;
  2.  
  3. uses Math;
  4.  
  5. Const smax = 99;
  6. Type simple = array[0..smax] of smallint;
  7. Var data, tempData:simple; i, n:smallint;
  8.  
  9. procedure writeArr(arr:simple);
  10. Var i:smallint;
  11. begin
  12. for i:=0 to n-1 do write(arr[i], ' ');
  13. writeln();
  14. end;
  15.  
  16. function sumOfDigitsInNum(num:smallint):smallint;
  17. Var i, t:smallint;
  18. begin
  19. t:=0;
  20. for i:=0 to 5 do t := t + ((abs(num) div floor(power(10, i))) -
  21. 10*(abs(num) div floor(power(10, i+1))));
  22. sumOfDigitsInNum:=t;
  23. end;
  24.  
  25. procedure swap(Var a, b:smallint);
  26. Var t:smallint;
  27. begin
  28. t:=a;
  29. a:=b;
  30. b:=t;
  31. end;
  32.  
  33. function customSort(data, tempData:simple):simple;
  34. Var i, j, t:smallint;
  35. begin
  36. for i:=0 to n-1 do begin
  37. for j:=0 to n-2 do begin
  38. if tempData[j]>tempData[j+1] then begin
  39. swap(tempData[j], tempData[j+1]);
  40. swap(data[j], data[j+1]);
  41. end;
  42.  
  43. if (tempData[j]=tempData[j+1]) and (data[j]>data[j+1]) then begin
  44. swap(tempData[j], tempData[j+1]);
  45. swap(data[j], data[j+1]);
  46. end;
  47. end;
  48. end;
  49. customSort:=data;
  50. end;
  51.  
  52. begin
  53. read(n);
  54. for i:=0 to n-1 do read(data[i]);
  55. for i:=0 to n-1 do tempData[i]:=sumOfDigitsInNum(data[i]);
  56. writeArr(customSort(data, tempData));
  57. end.
Success #stdin #stdout 0.01s 5260KB
stdin
2
5 -5
stdout
-5 5