Created
December 30, 2018 07:09
-
-
Save rkhang7/13da0a11502ec7d63efb088ec290e004 to your computer and use it in GitHub Desktop.
Sử dụng lại bài 29. Hãy viết hàm tìm số chẵn lớn nhất nhỏ hơn mọi giá trị lẻ có trong mảng một chiều. Dùng bất kỳ ngôn ngữ nào bạn thích. Cách nộp bài xem "Hướng dẫn nộp bài
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <conio.h> | |
void NhapMang(int a[], int n) | |
{ | |
int i; | |
for (i = 0; i < n;i++){ | |
printf("a[%d]= ", i); | |
scanf("%d", &a[i]); | |
} | |
} | |
void DoiViTri(int &b, int &c) | |
{ | |
int d; | |
d=b; | |
b=c; | |
c=d; | |
} | |
void SapXepMang(int a[], int n) | |
{ | |
int i,j; | |
for(i = 0;i < n;i++) | |
for(j = n-1; j > i ;j--){ | |
if(a[i] > a[j]) DoiViTri(a[i],a[j]); | |
} | |
} | |
int TimSoLeMax(int a[], int n) | |
{ | |
int i, max = a[0]; | |
for (i = 0; i < n;i++){ | |
if(a[i] % 2 != 0){ | |
if ( max < a[i]){ | |
max =a[i]; | |
} | |
} | |
} | |
return max; | |
} | |
void XuatMang(int a[], int n) | |
{ | |
int i; | |
for (i = 0; i < n;i++){ | |
printf("%d\t",a[i]); | |
} | |
} | |
int TimSo(int a[], int n) | |
{ | |
int sct,i; | |
for (i = 0; i < n;i++){ | |
if (a[i] % 2 ==0){ | |
if(a[i] > TimSoLeMax(a,n)){ | |
sct = a[i]; | |
break; | |
} | |
} | |
} | |
return sct; | |
} | |
int main() | |
{ | |
int n,sole,socantim; | |
printf("Nhap so luong phan tu cua mang: "); scanf("%d", &n); | |
int a[n]; | |
NhapMang(a,n); | |
TimSoLeMax(a,n); | |
SapXepMang(a,n); | |
if(TimSo(a,n)) printf("So can tim la %d ",TimSo(a,n)); | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment