Advertisement
GerexD

dinamikus-novekvo.reszsorozat

Mar 7th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. void beolvas(int a[],int &n)
  5. {
  6. cout<<"n=";cin>>n;
  7. for(int i=1;i<=n;i++)
  8. cin>>a[i];
  9. }
  10. void visszakeres(int ind, int index[], int a[])
  11. {
  12. if(index[ind]==0) cout<<a[ind]<<" ";
  13. else
  14. {
  15. visszakeres(index[ind], index, a);
  16. cout<<a[ind]<<" ";
  17. }
  18.  
  19. }
  20. int main()
  21. {
  22. int a[30],hossz[30],index[30],n;
  23. beolvas(a,n);
  24. hossz[1]=1; index[1]=0;
  25. for(int i=2;i<=n;i++)
  26. {
  27. int max=0,ind=0;
  28. for(int j=1;j<=i-1;j++)
  29. if(a[j]<a[i] && hossz[j]>max)
  30. {
  31. max=hossz[j];
  32. ind=j;
  33.  
  34. }
  35. hossz[i]=max+1;
  36. index[i]=ind;
  37. }
  38. /** for(int i=1;i<=n;i++)
  39. cout<<hossz[i]<<" ";
  40. cout<<endl;
  41. for(int i=1;i<=n;i++)
  42. cout<<index[i]<<" ";
  43. cout<<endl;
  44. */
  45. int maxim=0,ind;
  46.  
  47. for(int i=1;i<=n;i++)
  48. if(a[i]>maxim)
  49. {
  50. maxim=hossz[i];
  51. ind=i;
  52. }
  53. cout<<"A leghosszabb novekvo reszsorozat hossza "<<maxim<<endl;
  54. visszakeres(ind, index, a);
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement