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
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
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2020-09-21 11:11:04
#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 N : ");
scanf("%d", &N);
int t[N], i;
for(i=0; i<N; i++){
printf("\nNhap t[%d] = ", i);
scanf("%d", &t[i]);
}
for(i=0; i<N; i++){
if(t[i] %2 == 0){
printf("\n%d", t[i]);
}
}
for(i=0; i<N; i++){
if(t[i] % 2 != 0){
printf("\n%d", t[i]);
}
}
return 0;
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2020-09-21 09:46:37
#i
#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 N: ");
scanf("%d", &N);
int t[N];
int i;
for(i=0;i<N;i++) {
printf("\nNhap N[%d] = ", i);
scanf("%d", &t[i]);}
for(i=0;i<N;i++){
if(t[i]%2==0){
printf("%d;",t[i]);
}
}
for(i=0;i<N;i++){
if(t[i]%2!=0){
printf("%d;", t[i]);
}
}
return 0;
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2020-09-21 09:17:08
#include<stdio.h>
#include<stdlib.h>
//Bai tap tach chan le
int main(){
int *p,n,i,j;
printf("\nNhap n: ");
scanf("%d",&n);
p=(int)malloc(n * sizeof(int));
printf("\nNhap gia tri cac phan tu:");
for(i=0;i<n;i++){
printf("\np[%d]= ",i);
scanf("%d",p+i);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(p[j] % 2 == 0){
int temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
for(i=0;i<n;i++){
printf("%d ",*(p+i));
}
}
![Đặng Trần Nhật Minh [T2008A]](https://www.gravatar.com/avatar/ee8dc5a777ad26f3a962e86c233437cf.jpg?s=80&d=mm&r=g)
Đặng Trần Nhật Minh
2020-09-21 09:12:07
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, a[100006];
printf("\nNhap n: "); scanf("%d", &n);
for (int i = 0; i < n; i++) printf("\Nhap a[%d] = ", i), scanf("%d", &a[i]);
int l = 0, r = n - 1;
for (int i = l; i <= r; i++) {
while (a[l] % 2 == 0) l++;
while (a[r] % 2 == 1) r--;
if (a[i] % 2 != 0) {
int temp = a[i];
a[i] = a[r];
a[r] = temp;
r--;
}
}
for (int i = 0; i < n; i++) printf("\na[%d] = %d", i, a[i]);
}
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-09-21 08:52:08
#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,n;
printf("\nNhap so nguyen N:");
scanf("%d",&n);
int t[n];
for(i=0;i<=n-1;i++){
printf("\nNhap t[%d]:",i);
scanf("%d",&t[i]);
}
for(i=0;i<=n-1;i++){
if(t[i]%2==0){
printf("%d ",t[i]);
}
}
for(i=0;i<=n-1;i++){
if(t[i]%2!=0){
printf("%d ",t[i]);
}
}
return 0;
}