Advertisement
neversmile

tong hop 2

Jun 20th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. http://forum.congthuong.net/threads/tong-hop-tat-ca-bai-tap-trong-sach-bai-tap-ky-thuat-lap-trinh.1746/
  2.  
  3.  
  4. tìm “giá trị lớn nhất” trong mảng 1 chiều các số thực
  5.  
  6.  
  7. float lonnhat(float a[],int n) {
  8. float max = a[0];
  9. for (int i=1; i<n; i++)
  10. {
  11. max = (max<a[i])?a[i]:max;
  12. }
  13. return max;
  14. }
  15.  
  16.  
  17. //chan o dau, 0 o giua, le o cuoi,
  18. #include <stdio.h>
  19. /*
  20. */
  21. void Swap(int *a, int *b)
  22. {
  23. int tmp = *a;
  24. *a = *b;
  25. *b = tmp;
  26. }
  27.  
  28. void Process(int a[], int sz)
  29. {
  30. //Chuyen chan ve dau, le xuong cuoi
  31. int t1 = 0, t2 = sz-1;
  32. while (t1<t2)
  33. {
  34. while (t1<t2 && a[t1]%2==0) t1++;
  35. while (t1<t2 && a[t2]%2==1) t2--;
  36. Swap(&a[t1],&a[t2]);
  37. }
  38.  
  39. // Chuyen 0 ra giua
  40. int i, j=t1-1;
  41. for (i=0; i<j; i++)
  42. {
  43. while (a[j]==0) j--;
  44. if (a[i]==0)
  45. {
  46. Swap(&a[i],&a[j]);j--;
  47. }
  48. }
  49.  
  50. //Sort chan tang
  51. int k;
  52. for (i=0; i<j; i++)
  53. for (k=i+1; k<=j; k++)
  54. if (a[k]<a[i]) Swap(&a[k],&a[i]);
  55.  
  56. //Sort chan giam
  57. for (i=t2; i<sz-1; i++)
  58. for (k=i+1; k<sz; k++)
  59. if (a[k]>a[i]) Swap(&a[k],&a[i]);
  60. }
  61.  
  62. void Input(int a[], int sz)
  63. {
  64. int i;
  65. for (i=0; i<sz; i++)
  66. {
  67. printf(" a[%d] = ",i); scanf("%d",&a[i]);
  68. }
  69. }
  70.  
  71. void Show(int a[], int sz)
  72. {
  73. int i;
  74. for (i=0; i<sz; i++)
  75. printf("%6d",a[i]);
  76. printf("\n");
  77. }
  78.  
  79. int main()
  80. {
  81. int a[10], sz=8;
  82. Input(a,sz);
  83. Process(a,sz);
  84. Show(a,sz);
  85. return 0;
  86. }
  87.  
  88.  
  89.  
  90. voidhoanvi(int&a,int&b)
  91. {
  92. inttemp=a;
  93. a=b;
  94. b=temp;
  95. }
  96. voidchandaulecuoi(inta[],intn)
  97. {
  98. for(inti=0;i<n;i++)
  99. {
  100. for(intj=i+1;j<n;j++)
  101. if(a[j]%2==0)
  102. {
  103. hoanvi(a[i],a[j]);
  104. break;
  105. }
  106. for(intj=n-1-i;j>=0;j--)
  107. if(a[j]%2!=0)
  108. {
  109. hoanvi(a[j],a[n-1-i]);
  110. break;
  111. }
  112. }
  113. }
  114.  
  115.  
  116. delete an lelement from array
  117.  
  118. #include <stdio.h>
  119.  
  120. int main()
  121. {
  122. int array[100], position, c, n;
  123.  
  124. printf("Enter number of elements in array\n");
  125. scanf("%d", &n);
  126.  
  127. printf("Enter %d elements\n", n);
  128.  
  129. for ( c = 0 ; c < n ; c++ )
  130. scanf("%d", &array[c]);
  131.  
  132. printf("Enter the location where you wish to delete element\n");
  133. scanf("%d", &position);
  134.  
  135. if ( position >= n+1 )
  136. printf("Deletion not possible.\n");
  137. else
  138. {
  139. for ( c = position - 1 ; c < n - 1 ; c++ )
  140. array[c] = array[c+1];
  141.  
  142. printf("Resultant array is\n");
  143.  
  144. for( c = 0 ; c < n - 1 ; c++ )
  145. printf("%d\n", array[c]);
  146. }
  147.  
  148. return 0;
  149. }
  150.  
  151. sap xep
  152.  
  153. voidhoanvi(int&a,int&b)
  154. {
  155. inttemp=a;
  156. a=b;
  157. b=temp;
  158. }
  159. voidchandaulecuoi(inta[],intn)
  160. {
  161. for(inti=0;i<n;i++)
  162. {
  163. for(intj=i+1;j<n;j++)
  164. if(a[j]%2==0)
  165. {
  166. hoanvi(a[i],a[j]);
  167. break;
  168. }
  169. for(intj=n-1-i;j>=0;j--)
  170. if(a[j]%2!=0)
  171. {
  172. hoanvi(a[j],a[n-1-i]);
  173. break;
  174. }
  175. }
  176. }
  177.  
  178.  
  179. them gia tri
  180.  
  181.  
  182. void themgiatri(int a[], int &n, int x, int k)
  183. {
  184. for (i=n; i>k; i--)
  185. {
  186. a[i] = a[i-1];
  187. }
  188. a[k] = x;
  189. n++;
  190. }
  191.  
  192.  
  193. xoa max
  194.  
  195. void xoavitri(float a[], int &n, int k)
  196. {
  197. for (int i=k; i<n; i++)
  198. {
  199. a[i] = a[i+1];
  200. }
  201. n--;
  202. }
  203. float lonnhat(float a[], int n)
  204. {
  205. float max = a[0];
  206. for (int i=1; i<n ; i++)
  207. {
  208. max=(max<a[i])?a[i]:max;
  209. }
  210. return max;
  211. }
  212. void xoamang(float a[], int &n)
  213. {
  214. float max = lonnhat(a,n);
  215. for (int i=0; i<n; i++)
  216. {
  217. if (a[i]==max)
  218. {
  219. xoavitri(a,n,i);
  220. i--;
  221. }
  222. }
  223. }
  224.  
  225.  
  226. maxximum number
  227. #include <stdio.h>
  228.  
  229. int main()
  230. {
  231. int array[100], maximum, size, c, location = 1;
  232.  
  233. printf("Enter the number of elements in array\n");
  234. scanf("%d", &size);
  235.  
  236. printf("Enter %d integers\n", size);
  237.  
  238. for (c = 0; c < size; c++)
  239. scanf("%d", &array[c]);
  240.  
  241. maximum = array[0];
  242.  
  243. for (c = 1; c < size; c++)
  244. {
  245. if (array[c] > maximum)
  246. {
  247. maximum = array[c];
  248. location = c+1;
  249. }
  250. }
  251.  
  252. printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
  253. return 0;
  254. }
  255.  
  256.  
  257.  
  258. xoa am
  259.  
  260. void xoavitri(float a[], int &n, int k)
  261. {
  262. for (int i=k; i<n; i++)
  263. {
  264. a[i] = a[i+1];
  265. }
  266. n--;
  267. }
  268. void xoamang(float a[], int &n)
  269. {
  270. for (int i=0; i<n; i++)
  271. {
  272. if (a[i]<0)
  273. {
  274. xoavitri(a,n,i);
  275. i--;
  276. }
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement