Advertisement
Guest User

Câu 3: Có một mảng số nguyên không trùng có n phần tử. với n

a guest
Feb 27th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 100
  4. //nhập mảng 1 chiều
  5. void Input(int a[MAX],int n)
  6. {
  7.  
  8. for(int i=0;i<n;i++)
  9. {
  10. cout<<"a["<<i<<"]:";
  11. cin>>a[i];
  12. }
  13. }
  14. //xuất mảng 1 chiều
  15. void Output(int a[MAX],int n)
  16. {
  17. for(int i=0;i<n;i++)
  18. {
  19. cout<<a[i]<<"\t";
  20. }
  21. }
  22. //sắp xếp tăng dần
  23. int sort(int a[MAX],int n)
  24. {
  25. if(n==1){
  26. return a[n-1];
  27. }
  28. else{
  29. sort(a,n-1);
  30. if(a[n-1] > a[n-2])
  31. {
  32. int temp=a[n-1];
  33. a[n-1]=a[n-2];
  34. a[n-2]=temp;
  35. sort(a,n-1);
  36. }
  37. }
  38. }
  39. //lấy 3 phần tử lớn nhất
  40. void max3(int a[MAX],int b[3],int n)
  41. {
  42. for(int i=0;i<3;i++)
  43. {
  44. b[i]=a[i];
  45. cout<<b[i]<<"\t";
  46. }
  47.  
  48. }
  49.  
  50. int main()
  51. {
  52. int n;
  53. int a[MAX],b[3];
  54.  
  55. cout<<"Nhap so luong phan tu: ";
  56. cin>>n;
  57.  
  58. Input(a,n);
  59. sort(a,n);
  60. max3(a,b,n);
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement