Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int suma_cifrelor(int n);
- int main()
- {
- int n;
- cin >> n;
- int numere[n];
- for (int i = 0; i < n; i++) {
- cin >> numere[i];
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n-1; j++) {
- int sumaPozitieCurenta = suma_cifrelor(numere[j]);
- int sumaPozitieViitoare = suma_cifrelor(numere[j+1]);
- if ( sumaPozitieCurenta > sumaPozitieViitoare) {
- int aux = numere[j];
- numere[j] = numere[j+1];
- numere[j+1] = aux;
- } else if (sumaPozitieCurenta == sumaPozitieViitoare) {
- if (numere[j] > numere[j+1]) {
- int aux = numere[j];
- numere[j] = numere[j+1];
- numere[j+1] = aux;
- }
- }
- }
- }
- for (int i = 0; i < n; i++) {
- cout << numere[i] << " ";
- }
- return 0;
- }
- int suma_cifrelor(int n) {
- int suma = 0;
- while (n > 0) {
- suma += n % 10;
- n = n / 10;
- }
- return suma;
- }
Advertisement
Add Comment
Please, Sign In to add comment