Advertisement
nguyenhappy92

Tính tổng các phần tử “cực trị” trong mảng

Dec 24th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. // Tinh tong cuc tri cua mang
  2. // Khai bao cac ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #define SIZE 100
  6. void nhap(int a[],int &n)
  7. {
  8. scanf("%d",&n);
  9. for(int i=0;i<n;i++)
  10. {
  11. scanf("%d",&a[i]);
  12. }
  13. }
  14. long tongcuctri(int a[],int n)
  15. {
  16. long Tong=0;
  17. for(int i = 0; i < n; i++)
  18. {
  19. if(i == 0 && a[i] != a[i + 1]) // xét dau chuoi
  20. {
  21. Tong += a[i];
  22. }
  23. else if(i == n - 1 && a[i] != a[i - 1]) // xét duôi, vi tri nam cuoi chuoi
  24. {
  25. Tong += a[i];
  26. }
  27. else if((a[i] < a[i + 1] && a[i] < a[i - 1]) || (a[i] > a[i + 1] && a[i] > a[i - 1]))
  28. {
  29. Tong += a[i];
  30. }
  31. }
  32. return Tong;
  33. }
  34. void main()
  35. {
  36. int a[SIZE],n;
  37. nhap(a,n);
  38. printf("%ld",tongcuctri(a,n));
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement