Advertisement
AlexandruDu

Subsir de suma maxima

Dec 8th, 2020
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int a[100],b[100],n,m;
  6.  
  7. void citire()
  8. {
  9.     cout<<"n=";cin>>n;
  10.     for(int i=0;i<n;i++)
  11.     {
  12.         cout<<"v["<<i<<"]=";
  13.         cin>>a[i];
  14.     }
  15. }
  16.  
  17. void greedy(int &s)
  18. {
  19.     for(int i=0;i<n;i++)
  20.         if(a[i]>=0)
  21.         {
  22.             b[m]=a[i];
  23.             m++;
  24.             s+=a[i];
  25.         }
  26. }
  27.  
  28. void afisare(int v[100],int t)
  29. {
  30.     cout<<endl;
  31.     for(int i=0;i<t;i++)
  32.         cout<<v[i]<<" ";
  33. }
  34.  
  35. int main()
  36. {
  37.     int s=0;
  38.     citire();
  39.     greedy(s);
  40.     afisare(a,n);
  41.     afisare(b,m);
  42.     cout<<endl<<s;
  43.     return 0;
  44. }
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement