Advertisement
nguyenhappy92

Tìm ước chung lớn nhất của hai phần tử nhỏ nhất trong mảng

Dec 10th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. // Tim uoc chung nho nhat
  2. // Khai bao ham thu vien neu co
  3. #include <stdio.h>
  4.  
  5. void Swap(int &a, int &b)// Hoan vi sap xep
  6. {
  7. int temp = a;
  8. a = b;
  9. b = temp;
  10. }
  11.  
  12. void SoSanh(int n, int B[])// so sanh dau
  13. {
  14. if (n < B[0])
  15. Swap(n, B[0]);
  16. if (n < B[1])
  17. Swap(n, B[1]);
  18. }
  19.  
  20. int UCLN(int a, int b)// tim uoc chung
  21. {
  22. do
  23. {
  24. if (a == b) return a;
  25. else if (a > b)
  26. if (a % b == 0) return b;
  27. else a = a % b;
  28. else
  29. if (b % a == 0) return a;
  30. else b = b % a;
  31. } while (1);
  32. }
  33.  
  34. int TimMin(int A[], int B[])// tim min
  35. {
  36. int n;
  37. scanf("%d", &n);
  38.  
  39. scanf("%d", &A[0]);
  40. B[0] = B[1] = A[0];
  41. for (int i=1; i<n; i++)
  42. {
  43. scanf("%d", &A[i]);
  44. SoSanh(A[i],B);
  45. }
  46. return UCLN(B[0],B[1]);
  47. }
  48.  
  49. void main()
  50. {
  51. int A[10000] = {0};
  52. int B[2] = {0};
  53. printf("%d", TimMin(A,B));
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement