Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int sum(int n){
- int sum = 0;
- while(n){
- sum+=n%10;
- n/=10;
- }
- return sum;
- }
- bool compare(int a, int b){
- int sa = sum(a), sb = sum(b);
- if(sa < sb)
- return true; //sortat ok
- if(sa > sb)
- return false; //sortat invers
- // A rămas cazul în care au sumele egale, verificăm numărul efectiv
- if(a <= b)
- return true; // E ok
- return false; // Greșit
- }
- int main(){
- int n, i, j, aux;
- cin>>n;
- short int v[n];
- for(i = 0; i < n; i++){
- cin>>v[i];
- }
- int changes = 0;
- do{
- for(i = 0; i < n; i++){
- for(j = i+1; j < n; j++){
- if(compare(v[i], v[j]) == false){
- aux = v[i];
- v[i] = v[j];
- v[j] = aux;
- changes++;
- }
- }
- }
- }while(changes == 0);
- for (int i = 0; i < n; i++) {
- cout<<v[i]<<" ";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement