Code for fun

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.6 kB
7
Indexable
Never
#include<iostream>
#include<cmath>
using namespace std;
void input(int *a, int n)
{
	for(int i = 0;i < n; i++)
	{
		cout<<"a[" << i << "] = "; cin >> *(a+i);
	}
}
void output(int *a, int n)
{
	for(int i = 0; i < n; i++)
	{
		cout << *(a+i) << " ";
	}
}
int gtln(int *a, int n)
{
	int max = *a;
	for(int i = 0; i < n; i++)
	{
		if (*(a+i) > max)
			max = *(a+i);
	}
	return max;
}
int gtnn(int *a, int n)
{
	int min = *a;
	for (int i = 0; i < n; i++)
	{
		if (*(a+i) < min)
		{
			min = *(a+i);
		}
	}
	return min;
}
void sapxep(int *a, int n)
{
	for (int i = 0; i < 4; i++)
	{
		for (int j = i+1; j < 5; j++)
		{
			if (*(a+i) < *(a+j))
				swap(a[i], a[j]);
		}
	}
}
bool ktnt(int n) // kiem tra nguyen to
{
    if (n < 2)
        return false;
    for (int i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;
    return true;
}
bool ktcp(int n)
{
	int sqr = sqrt(n);
	if (sqr*sqr == n)
		return true;
	return false;
}
int main()
{
	int n;
	cout << "n = "; cin >> n;
	int *a = new int[n];
	input(a, n);
	cout << "Mang vua nhap vao la: ";
	output(a, n);
	cout << "\n\nPhan tu lon nhat trong mang la: " << gtln(a, n);
	cout << "Phan tu nho nhat trong mang la: " << gtnn(a, n);
	sapxep(a, n);
	cout << "\n\nMang sau khi sap xep la: "; output(a, n);
	cout << "\n\nCac so nguyen to trong mang a la: ";
	for (int i = 0; i < n; i++)
	{
		if (ktnt(a[i]))
			cout << a[i] << " ";
	}
	cout << "\n\nCac so chinh phuong trong mang a la: ";
	for (int i = 0; i < n; i++)
	{
		if (ktcp(a[i]))
			cout << a[i] << " ";
	}
	return 0;
}