Advertisement
halexandru11

atestat_14.cpp

Nov 25th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // determina cea mai mare cifra a numarului x
  6. unsigned int cifra_maxima(unsigned int x) {
  7.     unsigned int max_cif = 0;
  8.     while(x) {
  9.         max_cif = (x%10 > max_cif ? x%10 : max_cif);
  10.         x /= 10;
  11.     }
  12.     return max_cif;
  13. }
  14.  
  15. // determina cmmdc dintre a si b
  16. unsigned int cmmdc(unsigned int a, unsigned int b) {
  17.     if(a*b == 0) {
  18.         return a+b;
  19.     }
  20.     return cmmdc(b, a%b);
  21. }
  22.  
  23. int main() {
  24.     unsigned int n;
  25.     unsigned int a[101];
  26.     // citesc datele de intrare
  27.     cin >> n;
  28.     for(int i = 0; i < n; ++i) {
  29.         cin >> a[i];
  30.     }
  31.  
  32.     // afisez cifra maxima a fiecarui numar
  33.     for(int i = 0; i < n; ++i) {
  34.         cout << cifra_maxima(a[i]) << " ";
  35.     }
  36.     cout << "\n";
  37.  
  38.     // calculez cmmdc a tuturor elementelor din vector
  39.     unsigned int div_com = a[0];
  40.     for(int i = 1; i < n; ++i) {
  41.         div_com = cmmdc(div_com, a[i]);
  42.     }
  43.     cout << div_com;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement