Đề bài :
#include <iostream>
#include <math.h>
using namespace std;
int i,n,j,t;
int a[1000];
// cong viec 1
void nhapdl()
{
do{
cout<<"Nhap So luong phan tu cua day (5<=n<=50): ";cin>>n;
}while((n<5)||(n>50));
for(i=1;i<=n;i++)
{
cout<<"a["<<i<<"]= ";
cin>>a[i];
}
}
//in du lieu
void indl()
{
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
//sap xep giam
void sapxepgiam()
{
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if (a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
// in so lon nhat
void solonnhat()
{ int max=a[1];
for(i=1;i<n;i++)
if (a[i]>max)
max=a[i];
cout<<"so lon nhat cua mang la: "<<max<<endl;
}
// tim so hoan hao
// kiem tra so hoan hao
bool checkhh(int n){
int sum = 0;//khai bao biem sum
for(int i=1;i<=n/2;i++){
if(n%i==0)
sum+=i;
}
if(sum==n) return true; // tra ve true
return false;
}
// dem so luong va in shh
void inhoanhao()
{ int dem=0;
for(i=1;i<=n;i++)
if(checkhh(a[i])){
dem++;
}
if (dem>0){
cout<<"cac so hoan hao la: ";
for(i=1;i<=n;i++)
if(checkhh(a[i]))
cout<<a[i]<<" ";
}
else cout<<"day khong co so hoan hao"<<endl;
}
//in nguyen duong nho nhat
void NguyenDuongMin() {
int min;
for(i=0; i<n; i++) {
if(a[i]>0) {
min = a[i];
break;
}
}
for(i=1; i<=n; i++) {
if(a[i]>0 && a[i]<min)
min = a[i];
}
cout<<min<<endl;
}
int main() {
nhapdl();
indl();
sapxepgiam();
cout<<"Mang sau khi sap xep giam dan la: "<<endl;
indl();
solonnhat();
inhoanhao();
cout<<"so nguyen duong nho nhat: "<<endl;
NguyenDuongMin();
}