Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://forum.congthuong.net/threads/tong-hop-tat-ca-bai-tap-trong-sach-bai-tap-ky-thuat-lap-trinh.1746/
- tìm “giá trị lớn nhất” trong mảng 1 chiều các số thực
- float lonnhat(float a[],int n) {
- float max = a[0];
- for (int i=1; i<n; i++)
- {
- max = (max<a[i])?a[i]:max;
- }
- return max;
- }
- //chan o dau, 0 o giua, le o cuoi,
- #include <stdio.h>
- /*
- */
- void Swap(int *a, int *b)
- {
- int tmp = *a;
- *a = *b;
- *b = tmp;
- }
- void Process(int a[], int sz)
- {
- //Chuyen chan ve dau, le xuong cuoi
- int t1 = 0, t2 = sz-1;
- while (t1<t2)
- {
- while (t1<t2 && a[t1]%2==0) t1++;
- while (t1<t2 && a[t2]%2==1) t2--;
- Swap(&a[t1],&a[t2]);
- }
- // Chuyen 0 ra giua
- int i, j=t1-1;
- for (i=0; i<j; i++)
- {
- while (a[j]==0) j--;
- if (a[i]==0)
- {
- Swap(&a[i],&a[j]);j--;
- }
- }
- //Sort chan tang
- int k;
- for (i=0; i<j; i++)
- for (k=i+1; k<=j; k++)
- if (a[k]<a[i]) Swap(&a[k],&a[i]);
- //Sort chan giam
- for (i=t2; i<sz-1; i++)
- for (k=i+1; k<sz; k++)
- if (a[k]>a[i]) Swap(&a[k],&a[i]);
- }
- void Input(int a[], int sz)
- {
- int i;
- for (i=0; i<sz; i++)
- {
- printf(" a[%d] = ",i); scanf("%d",&a[i]);
- }
- }
- void Show(int a[], int sz)
- {
- int i;
- for (i=0; i<sz; i++)
- printf("%6d",a[i]);
- printf("\n");
- }
- int main()
- {
- int a[10], sz=8;
- Input(a,sz);
- Process(a,sz);
- Show(a,sz);
- return 0;
- }
- voidhoanvi(int&a,int&b)
- {
- inttemp=a;
- a=b;
- b=temp;
- }
- voidchandaulecuoi(inta[],intn)
- {
- for(inti=0;i<n;i++)
- {
- for(intj=i+1;j<n;j++)
- if(a[j]%2==0)
- {
- hoanvi(a[i],a[j]);
- break;
- }
- for(intj=n-1-i;j>=0;j--)
- if(a[j]%2!=0)
- {
- hoanvi(a[j],a[n-1-i]);
- break;
- }
- }
- }
- delete an lelement from array
- #include <stdio.h>
- int main()
- {
- int array[100], position, c, n;
- printf("Enter number of elements in array\n");
- scanf("%d", &n);
- printf("Enter %d elements\n", n);
- for ( c = 0 ; c < n ; c++ )
- scanf("%d", &array[c]);
- printf("Enter the location where you wish to delete element\n");
- scanf("%d", &position);
- if ( position >= n+1 )
- printf("Deletion not possible.\n");
- else
- {
- for ( c = position - 1 ; c < n - 1 ; c++ )
- array[c] = array[c+1];
- printf("Resultant array is\n");
- for( c = 0 ; c < n - 1 ; c++ )
- printf("%d\n", array[c]);
- }
- return 0;
- }
- sap xep
- voidhoanvi(int&a,int&b)
- {
- inttemp=a;
- a=b;
- b=temp;
- }
- voidchandaulecuoi(inta[],intn)
- {
- for(inti=0;i<n;i++)
- {
- for(intj=i+1;j<n;j++)
- if(a[j]%2==0)
- {
- hoanvi(a[i],a[j]);
- break;
- }
- for(intj=n-1-i;j>=0;j--)
- if(a[j]%2!=0)
- {
- hoanvi(a[j],a[n-1-i]);
- break;
- }
- }
- }
- them gia tri
- void themgiatri(int a[], int &n, int x, int k)
- {
- for (i=n; i>k; i--)
- {
- a[i] = a[i-1];
- }
- a[k] = x;
- n++;
- }
- xoa max
- void xoavitri(float a[], int &n, int k)
- {
- for (int i=k; i<n; i++)
- {
- a[i] = a[i+1];
- }
- n--;
- }
- float lonnhat(float a[], int n)
- {
- float max = a[0];
- for (int i=1; i<n ; i++)
- {
- max=(max<a[i])?a[i]:max;
- }
- return max;
- }
- void xoamang(float a[], int &n)
- {
- float max = lonnhat(a,n);
- for (int i=0; i<n; i++)
- {
- if (a[i]==max)
- {
- xoavitri(a,n,i);
- i--;
- }
- }
- }
- maxximum number
- #include <stdio.h>
- int main()
- {
- int array[100], maximum, size, c, location = 1;
- printf("Enter the number of elements in array\n");
- scanf("%d", &size);
- printf("Enter %d integers\n", size);
- for (c = 0; c < size; c++)
- scanf("%d", &array[c]);
- maximum = array[0];
- for (c = 1; c < size; c++)
- {
- if (array[c] > maximum)
- {
- maximum = array[c];
- location = c+1;
- }
- }
- printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
- return 0;
- }
- xoa am
- void xoavitri(float a[], int &n, int k)
- {
- for (int i=k; i<n; i++)
- {
- a[i] = a[i+1];
- }
- n--;
- }
- void xoamang(float a[], int &n)
- {
- for (int i=0; i<n; i++)
- {
- if (a[i]<0)
- {
- xoavitri(a,n,i);
- i--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement