Advertisement
Guest User

fdsa

a guest
Oct 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. ifstream f("expresie.in");
  8.  
  9. int a[100],b[100],n,m;
  10.  
  11. int v[100],k;
  12.  
  13. void citire(int a[], int &m, int b[],int &n)
  14.  
  15. {f>>m>>n;
  16.  
  17. for(int i=1;i<=m;i++)
  18.  
  19. f>>a[i];
  20.  
  21. for(int i=1;i<=n;i++)
  22.  
  23. f>>b[i];
  24.  
  25. }
  26.  
  27. // functie de afisare ale ni vect V cu K elemente
  28.  
  29. // va fii folosita pentru un vector cu k elemente
  30.  
  31. //ch va contine o litera care reprezinta denumirea, sau denumirea numelui vectorului care se afiseaza;
  32.  
  33. void afisare(int v[],int k,char ch)
  34.  
  35. {cout<<"Elementele vectorului "<<ch<<" sunt: "<<endl;
  36.  
  37. for(int i=1;i<=k;i++)
  38.  
  39. cout<<v[i]<<" ";
  40.  
  41. cout<<endl;
  42.  
  43. }
  44.  
  45. void ordonare(int v[], int k)
  46.  
  47. {int ok,aux;
  48.  
  49. do{
  50.  
  51. ok=1;
  52.  
  53. for(int i=1;i<=k-1;i++)
  54.  
  55. if(v[i]>v[i+1])
  56.  
  57. {aux=v[i];v[i]=v[i+1];v[i+1]=aux;ok=0;}
  58.  
  59. }while(ok==0);
  60.  
  61. }
  62.  
  63. void greedy(int a[],int m,int b[],int n)
  64.  
  65. { int e=0,i=1,j;
  66.  
  67. //inmultesc perechi de numere negative
  68.  
  69. while(a[i]<0 && b[i]<0 && i<=m)
  70.  
  71. {cout<<a[i]<<" "<<b[i]<<endl;
  72.  
  73. e=e+a[i]*b[i];
  74.  
  75. i++;
  76.  
  77. }
  78.  
  79. //inmultesc perechi de numere pozitive
  80.  
  81. i=m;
  82.  
  83. j=n;
  84.  
  85. while(a[i]>0 && i>=1)
  86.  
  87. {e=e+a[i]*b[j];
  88.  
  89. cout<<a[i]<<" "<<b[j]<<endl;
  90.  
  91. i--;j--;
  92.  
  93. }
  94.  
  95. cout<<endl<<"Valoarea maxima a expresiei E, este: "<<e;
  96.  
  97. }
  98.  
  99. int main()
  100.  
  101. {citire(a,m,b,n);
  102.  
  103. afisare(a,m,'a');
  104.  
  105. afisare(b,n,'b');
  106.  
  107. ordonare(a,m);
  108.  
  109. ordonare(b,n);
  110.  
  111. cout<<endl<<"Dupa sortare"<<endl;
  112.  
  113. afisare(a,m,'a');
  114.  
  115. afisare(b,n,'b');
  116.  
  117. cout<<endl;
  118.  
  119. greedy(a,m,b,n);
  120.  
  121. return 0;
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement