fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. bool compare(int a, int b) {
  7. return a % 10 == b % 10 ? a < b : a % 10 < b % 10;
  8. }
  9.  
  10. int main() {
  11. int n;
  12. cin >> n;
  13. vector<int> nums;
  14. while (n--) {
  15. int temp;
  16. cin >> temp;
  17. nums.push_back(temp);
  18. }
  19. sort(nums.begin(), nums.end(), compare);
  20. for (auto c : nums) {
  21. cout << c << ' ';
  22. }
  23. }
Success #stdin #stdout 0.01s 5348KB
stdin
2
1004
1002
stdout
1002 1004