Advertisement
huutho_96

Untitled

Mar 11th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "conio.h"
  3. #include "math.h"
  4.  
  5. int kiemtrasnt(int n)
  6. {
  7. for (int i = sqrt(float(n)); i >= 2; i--)
  8. //tai bat ki gia tri nao cua i ma n chia het thi do khong phai
  9. //snt nen return 0
  10. if (n % i == 0) return 0;
  11. //neu chay xong for thi tuc la n khong chia het cho gia tri i nao ca
  12. // va neu n > 1 thi la snt hoac khong phai snt neu n <= 1
  13. if (n > 1) return 1;
  14. else return 0;
  15. }
  16. //ham xoa co tham chieu n vi khi xoa 1 phan tu ta can giam
  17. //kich thuoc mang xuong. viec truyen tham chieu se thay doi gia tri n trong ham nay
  18. void xoa(int a[], int k, int &n)
  19. {
  20. for (int i = k; i < n - 1; i++)
  21. {
  22. a[i] = a[i + 1];
  23. }
  24. n--;
  25. }
  26.  
  27. //ham xoa so nugyen to
  28. void xoant(int a[], int &n)
  29. {
  30.  
  31. for (int i = 0; i < n; i++)
  32. //kiem tra a[i] co phai snt khong. neu phai thi goi ham xoa de xoa a[i]
  33. if (kiemtrasnt(a[i]) == 1) xoa(a, i, n);
  34. }
  35. void nhapmang(int *a, int n)
  36. {
  37. for (int i = 0; i < n; i++)
  38. {
  39. printf("nhap phan tu thu %d: ", i + 1);
  40. scanf("%d.\n", &a[i]);
  41. }
  42. }
  43. void xuatmang(int *a, int n)
  44. {
  45. for (int i = 0; i < n; i++)
  46. printf("%d\t", a[i]);
  47. }
  48. int main(void)
  49. {
  50. int n, b, k, m;
  51. printf("Nhap n: ");
  52. scanf_s("%d.\n", &n);
  53. int *a = new int[n];
  54. //nhap gia tri cho mang
  55. nhapmang(a, n);
  56. //xuat mang
  57. xuatmang(a, n);
  58.  
  59. //xoa so nguyen to trong mang neu co va xuat mang
  60. xoant(a, n);
  61. printf("\nmang sau khi xoa so nguyen to: \n");
  62. xuatmang(a, n);
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement