Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5. int n;
  6. string arr[1001];
  7. bool comp(string a, string b){
  8. if(a.size()!=b.size())
  9. return a.size()<b.size();
  10. else {
  11. int x=0,y=0;
  12. for(int i=0; i<a.size(); i++) {
  13. if(a[i]>=49 && a[i]<=57) {
  14. x+=a[i]-48;
  15. }
  16. }
  17. for(int i=0; i<b.size(); i++) {
  18. if(b[i]>=49 && b[i]<=57) {
  19. y+=b[i]-48;
  20. }
  21. }
  22. if(x == y) {
  23. if(a.compare(b)<0)
  24. return true;
  25. return false;
  26. }
  27. return x<y;
  28. }
  29. }
  30. int main() {
  31. cin>>n;
  32. for(int i=0; i<n; i++) {
  33. cin>>arr[i];
  34. }
  35. sort(arr, arr+n, comp);
  36. for(int i=0; i<n; i++) {
  37. cout<<arr[i]<<endl;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement