Advertisement
mihainan

Laboratorul 5 - Prob. 7

Nov 3rd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. /**
  2.  * Problema 7
  3.  * Laboratorul 5 - Seria CC
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. int transforma(int n)
  9. {
  10.     int nr, i, cif, c[10] = {0};
  11.     // c[i] = de cate ori apare cifra i in numar
  12.     while (n > 0) {
  13.         cif = n % 10;
  14.         c[cif]++;
  15.         n = n / 10;
  16.     }
  17.     nr = 0; // noul numar
  18.     if (c[0] != 0) {
  19.         // contine 0  => descrescator
  20.         for (cif = 9; cif >= 0; cif--) // pt fiecare cifra
  21.             for (i = 0; i < c[cif]; i++) // o adaugam la nr de cate ori apare
  22.                 nr = nr * 10 + cif;
  23.     }
  24.     else {
  25.         // nu contine 0 => crescator
  26.         for (cif = 1; cif <= 9; cif++)
  27.             for (i = 0; i < c[cif]; i++)
  28.                 nr = nr * 10 + cif;
  29.     }
  30.         return nr;
  31. }
  32.  
  33. int main()
  34. {
  35.     int n;
  36.     scanf("%d", &n);
  37.     printf("%d\n", transforma(n));
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement