By GokiSoft.com| 20:18 14/12/2021|
Lập Trình C

Tách phần tử chẵn + lẻ trong C - Lập trình mảng trong C - Lập trình C BT1909

Yêu cầu nhập vào mảng gồm N phần tử -> sau đó thực hiện đẩy các số chẵn lên đầu và các số lẻ về cuối mảng

Ví dụ : mảng gồm các phần tử : 1, 2, 2, 5, 6, 7, 8, 10, 12

Mang sau khi chuyển sẽ biến thành : 2, 2, 6, 8, 10, 12 ,1 , 5, 7

Liên kết rút gọn:

https://gokisoft.com/1909

Bình luận

avatar
TRẦN VĂN ĐIỆP [community,C1812L,C2002L,T2008A]
2020-12-16 02:22:52



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	//Bai 1
	int n;//so phan tu trong mang
	printf("\nNhap so phan tu n: ");
	scanf("%d", &n);
	int t[n];
	
	int i;
	for(i=0;i<n;i++) {
		printf("\nNhap phan tu t[%d] = ", i);
		scanf("%d", &t[i]);
	}
	
	//Su dung giai thuat gi de swap even & odd
	int oddFirstIndex = -1;
	for(i=0;i<n;i++) {
		if(t[i] % 2 == 0) {
			if(oddFirstIndex >= 0) {
				//doi tro vi tri so chan va so le
				int tmp = t[i];
				t[i] = t[oddFirstIndex];
				t[oddFirstIndex]= tmp;
				
				oddFirstIndex++;
			}
		} else {
			//Xac dinh vi tri xuat hien dau tien cua so le trong mang t.
			if(oddFirstIndex == -1) {
				oddFirstIndex = i;
			}
		}
	}
	for(i=0;i<n;i++) {
		printf("\nNhap phan tu t[%d] = %d", i, t[i]);
	}
	
	return 0;
}


avatar
Nguyễn Hữu Hiếu [T2008A]
2020-10-01 04:42:24



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int i, j, n, temp;
	printf("\n Nhap so phan tu cua mang n= ");
	scanf("%d", &n);
	int mang[n];
	for(i=0;i<n;i++) {
		printf("\n Nhap phan tu thu %d: ", i+1);
		scanf("%d", &mang[i]);
	}
	printf("\nCac phan tu cua mang la: ");
	for(i=0;i<n;i++) {
		printf("%d, ", mang[i]);
	}
	
	printf("\nCac phan tu cua mang sau khi sap xep: ");
	for(i=0;i<n-1;i++) {
		for(j=i+1;j<n;j++) {
			if (mang[j] % 2 == 0) {
				temp = mang[i];
				mang[i] = mang[j];
				mang[j] = temp;
			}
		}
	}
	
		for(i=0;i<n;i++) {
		printf("%d, ", mang[i]);
	}
	
	return 0;
}


avatar
Nguyễn Anh Vũ [T2008A]
2020-09-22 17:52:51



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	//bat dau code;
	int i, n, c;
		int c[n];
	printf("\nNhap N: ");
	scanf("%d", &n);
	
	for(int i=0,i<n-1,i++){
		printf("\nNhap c[%d]: ", i);
		scanf("%d", &c[i]);
		}
	for(int i=0 ;i<n-1; i++){
    	if (c[i] %2== 0);
		 printf("%d ",c[i]);
	}
	for(int i=0 ;i<n-1; i++){
    	if (c[i] %2 != 0);
		 printf("%d ",c[i]);
	}
	
}
	//ket thuc code;
	return 0;
}


avatar
Triệu Văn Lăng [T2008A]
2020-09-22 08:30:42



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int *p, n, i;
	printf("\nnhap n: "); scanf("%d",&n);
	p=(int*) malloc(n* sizeof (int));
	for(i=0;i<n;i++) {
		printf("\nnhap p[%d]=",i);
		scanf("%d", p+i);
	}
	for(i=0;i<n;i++){
		if(p[i]%2==0) {
			printf("%d",p[i]);
		}
	}
	for(i=0;i<n;i++) {
		if(p[i]%2 !=0) {
			printf("%d",p[i]);
		}
	}
	
	return 0;
}


avatar
Trần Văn Lâm [T2008A]
2020-09-22 06:10:26



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n;
	printf("\nNhap so n=");
	scanf("%d", &n);
	
	int h[n], i;
	for(i=0;i<n;i++){
		printf("\nNhap h[%d]=%d", i);
		scanf("%d", &h[i]);
	}
	for(i=0;i<n;i++){
		if(h[i]%2==0){
			printf("\n%d", h[i]);
		}
	}
	for(i=0;i<n;i++){
		if(h[i]%2!=0){
			printf("\n%d", h[i]);
		}
	}

	return 0;
}


avatar
nguyễn Sử [T2008A]
2020-09-22 05:59:11



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
int n,a;
	printf("Nhap n: ");
	scanf("%d", &n);
int i[n];
for(a=0;a<n;a++){	
	printf("\nNhap phan tu [%d]",a);
		 scanf("%d",& i[a]);}
	for( a = 1; a < n; a++) {
		if (i[a]%2==0){	
		printf("%d, ", i[a]);
		}}
			for( a = 1; a < n; a++) {
				if(i[a] % 2 != 0){
						printf("%d, ", i[a]);}}
	printf("\nMang da sap xep : ");	
	return 0;
}


avatar
Do Trung Duc [T2008A]
2020-09-22 04:52:17



//Nhap mang N phan tu, nhap gia tri tung phan tu, day cac phan tu so chan len dau, so le ve cuoi
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int temp,i,j,N;
	printf("Nhap N: ");
	scanf ( "%d", &N);
	
	int t[N];
	
	for(i=0; i<N; i ++){
		printf("Nhap phan tu t[%d] =", i);
		scanf("%d", &t[i]);
	}
	
	
	for(i=0; i<N-1;i++){
		for(j=i+1; j<N;j++){
			if(t[j] %2 ==0){
				temp = t[i];
				t[i] = t [j];
				t[j] = temp;	
			}	
		}	
	}
	printf("Day phan tu sau khi sap xep:");
	for(i=0; i<N ;i++){
		printf("%d", t[i]);
	}
		
	return 0;
}


avatar
Nguyên Phấn Đông [community]
2020-09-21 13:10:34



#include<stdio.h>
#include<stdlib.h>
int main(){
	int n, t[n];
		
	printf("Nhap N: "); scanf("%d",&n);
	for(int i=0 ;i<n; i++)	printf("Nhap phan tu t[%d]: ",i), scanf("%d", &t[i]);
	
    for(int i=0 ;i<n; i++){
    	if (t[i] %2== 0) printf("%d ",t[i]);
	}
	for(int i=0 ;i<n; i++){
    	if (t[i] %2 != 0) printf("%d ",t[i]);
	}
	return 0;
	
}


avatar
vuong huu phu [T2008A]
2020-09-21 12:50:25



#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int n,i,f;
	printf("\n nhap so phan tu ");
	scanf("%d",&n);
	int a[n];
	for(i=0;i<n;i++){
		printf("\n nhap phan tu %d =",i);
		scanf("%d",&a[i]);
	}
	for(i=0;i<=n-1;i++){
	if (a[i]%2==0){	
	printf(" %d ",a[i]);}	
	}
	for(f=0;f<=n-1;f++){
	if (a[f]%2!=0){	
	printf(" %d ",a[f]);}	
	}

	return 0;
}


avatar
An Văn Minh [T2008A]
2020-09-21 12:06:11



#include<stdio.h>
#include<conio.h>
int main(){
	int N;
	printf("\nNhap N:");
	scanf("%d", &N);
	int t[N],i;
	for(i=0;i<N;i++){
		printf("\nt[%d]:", i);
		scanf("%d", &t[i]);
	}
	for(int i=0;i<N;i++)
		for(int j=i;j<N;j++){
			if(t[j]%2==0){
				int m = t[i];
				t[i]=t[j];
				t[j]=m;
			}
		}
		printf("\nMang sau khi sap xep la:");
		for(int i=0;i<N;i++)
		printf("%d ", t[i]);
	return 0;
}