Advertisement
Guest User

Bài 3

a guest
Jul 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. //Nhận xét
  2. bài 3. em nghĩ sao mà em lấy mảng con có độ lớn là 3x3 vậy ?
  3. //Code
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<stdlib.h>
  7. int **Nhapmang(int *pn)
  8. {
  9. int i,j;
  10. int **a;
  11. printf("Nhap n: ");
  12. scanf("%d",pn);
  13.  
  14. a=(int**)malloc(*pn*sizeof(int));
  15. for(i=0;i<*pn;i++)
  16. {
  17. a[i]=(int*)malloc(*pn*sizeof(int));
  18. }
  19. for(i=0;i<*pn;i++)
  20. {
  21. for(j=0;j<*pn;j++)
  22. {
  23. printf("a[%d][%d]= ",i,j);
  24. scanf("%d",a[i]+j);
  25. }
  26. }
  27. return a;
  28. }
  29. int Kiemtra(int **a, int n, int xDong, int xCot, int yDong, int yCot)
  30. {
  31. int i,j;
  32. int t=0;
  33. for(i=xDong;i<=yDong;i++)
  34. {
  35. for(j=xCot;j<=yCot;j++)
  36. {
  37. t+=a[i][j];
  38. }
  39. }
  40. return t;
  41. }
  42. int Tim(int **a, int n)
  43. {
  44. int i,j,l,k,max=-2147483648;
  45. for(i=0;i<n;i++)
  46. {
  47. for(j=0;j<n;j++)
  48. {
  49. for(l=0;l<n;l++)
  50. {
  51. for(k=0;k<n;k++)
  52. {
  53. int t=Kiemtra(a,n,i,j,l,k);
  54. if(t>max)
  55. max=t;
  56. }
  57. }
  58. }
  59.  
  60. }
  61. return max;
  62. }
  63. int main()
  64. {
  65. int **a;
  66. int n;
  67. int i;
  68.  
  69. a=Nhapmang(&n);
  70. printf("Mang con co gia tri lon nhat la %d", Tim(a,n));
  71.  
  72. for(i=0;i<n;i++)
  73. {
  74. free(a[i]);
  75. a[i]=NULL;
  76. }
  77. free(a);
  78. a=NULL;
  79. _getch();
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement