nguyenhappy92

Tìm số nguyên tố có giá trị nhỏ nhất trong mảng

Nov 10th, 2015
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. // Tim so nguyen to nho nhat trong mang so nguye
  2. // Khai bao ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #define SIZE 100
  6.  
  7. void nhap(long a[],int &n)
  8. {
  9. scanf("%d",&n);
  10. for(int i=0;i<n;i++)
  11. {
  12. scanf("%ld",&a[i]);
  13. }
  14. }
  15. long soNT(long x)
  16. {
  17. int s=0;
  18. for(int i=1;i<=x;i++)
  19. {
  20. if(x%i==0)
  21. s++;
  22. }
  23. if(s==2)
  24. return 1;
  25. return 0;
  26. }
  27. long xuly(long a[],int n)
  28. {
  29. long min;
  30. int dem=0;
  31. for(int i=0;i<n;i++)
  32. {
  33. if(soNT(a[i])==1)
  34. {
  35. dem++;
  36. break;
  37. }
  38.  
  39. }
  40. if(dem==0)
  41. {
  42. return 0;
  43. }
  44. else
  45. {
  46. min=a[i];
  47. for(i=i+1;i<n;i++)
  48. {
  49. if((soNT(a[i])==1)&&(min>a[i]))
  50. min=a[i];
  51. }
  52. return min;
  53. }
  54. }
  55. void main()
  56. {
  57. long a[SIZE];
  58. int n;
  59. nhap(a,n);
  60. printf("%ld",xuly(a,n));
  61. }
Advertisement
Add Comment
Please, Sign In to add comment