Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. void swap(int** a, int** b) {
  8. int* temp = *a;
  9. *a = *b;
  10. *b = temp;
  11. }
  12.  
  13. int digitsSum(string x) {
  14. int sum = 0;
  15. while (x) {
  16. sum += x % 10;
  17. x /= 10;
  18. }
  19. return sum;
  20. }
  21.  
  22. int root(string x) {
  23. int temp;
  24. temp = digitsSum(x);
  25. while (temp > 9) {
  26. temp = digitsSum(temp);
  27. }
  28. return temp;
  29. }
  30.  
  31. void sort(int** a, int n) {
  32. int indOfMinElem;
  33. for (int i = 0; i < n - 1; ++i) {
  34. indOfMinElem = i;
  35. for (int j = i + 1; j < n; ++j) {
  36. if (a[j][1] < a[indOfMinElem][1]) {
  37. indOfMinElem = j;
  38. }
  39. else if (a[j][1] == a[indOfMinElem][1] && a[j][0] < a[indOfMinElem][0]) {
  40. indOfMinElem = j;
  41. }
  42. }
  43. swap(&a[i], &a[indOfMinElem]);
  44. }
  45. }
  46.  
  47.  
  48. int main() {
  49. int n;
  50. cin >> n;
  51. char*** numbers = new char** [n];
  52. for (int i = 0; i < n; ++i) {
  53. numbers[i] = new char[1001][2];
  54. }
  55. for (int i = 0; i < n; ++i) {
  56. cin >> numbers[i][0];
  57. numbers[i][1] = root(numbers[i][0]);
  58. }
  59. sort(numbers, n);
  60. for (int i = 0; i < n; ++i) {
  61. cout << numbers[i][0] << endl;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement