#include "iostream"
#include "string.h"
#include <math.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
int a[30];
int i, n, times = 1, andacbiet = 0, max;
int z = times;
cout << "Nhap so phan tu cua mang : ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "a[" << i << "]= ";
cin >> a[i];
if (i >= 1)
{
int j, tam, so1 = i;
for (i = 0; i < so1; i++) {
for (j = i + 1; j <= so1; j++)
{
if (a[i] > a[j]) {
tam = a[i]; a[i] = a[j]; a[j] = tam; times++;
}
}
}
if ((times - z) != 0)
{
andacbiet++;
if (andacbiet <= 5)
{
cout << "Ban da nhap mang khong theo thu tu tang dan!";
system("pause");
system("cls");
times = z;
i = -1;
cout << "Vui long nhap mang theo thu tu tang den!" << endl << "So lan da nhap sai la : " << andacbiet << endl;
}
else
{
system("cls");
cout << "Ban da nhap sai qua 5 lan!";
return 0;
}
}
}
}
if ((times - z) == 0)
{
system("cls");
cout << "Ban da nhap dung thu tu tang dan cua mang!" << endl;
for (i = 0; i < n; i++)
{
cout << "a[" << i << "]= " << a[i] << endl;
}
int so;
cout << "So muon them la : ";
cin >> so;
max = so;
for (i = 0; i < n; i++)
{
if (a[i] > so)
max = a[i];
}
if (max == so)
{
n++;
a[i] = max;
}
else
{
for (i = 0; i < n; i++)
{
if (a[i] > so)
{
n++;
int z;
for (z = n - 1; z > i; z--)
{
a[z] = a[z - 1];
}
a[i] = so;
break;
}
}
}
cout << "Day moi la : " << endl;
for (i = 0; i < n; i++)
cout << "a[" << i << "]= " << a[i] << endl;
}
return 0;
}