MangAmDuong
using System;
namespace MangAmDuong
{
public class Mang
{
static void Main()
{
Console.Write(" Nhap so phan tu mang: ");
int n=int.Parse(Console.ReadLine());
int[] A = new int[n];
Console.WriteLine("Tao mang nguon...");
Random r = new Random();
for (int i = 0; i < n; i++)
{
A[i] = r.Next(-n,n);
}
Console.WriteLine("Mang vua tao: ");
int DemAm = 0,DemDuong=0;
for (int i = 0; i < n; i++)
{
Console.Write("A[ {0} ] = {1}
",i,A[i]);
if(A[i]<=0) DemAm++;
else DemDuong++;
}
int[] Am = new int[DemAm];
int[] Duong = new int[DemDuong];
int t1 = 0, t2 = 0;
for (int i = 0; i < n; i++)
{
if (A[i] <= 0)
{
Am[t1] = A[i];
t1++;
}
else
{
Duong[t2] = A[i];
t2++;
}
}
Array.Sort(Am);
Array.Sort(Duong); Array.Reverse(Duong);
int[] kq = new int[n];
Array.Copy(Am, kq, DemAm);
for (int j = 0; j < DemDuong; j++)
{
kq[i] = Duong[j];
}
Console.WriteLine(" Mang da sap xep...");
for (int i = 0; i < n; i++)
{
Console.Write("KQ[ {0} ] = {1}
", i, kq[i]);
}
}
}
}
Bạn đang đọc truyện trên: Truyen247.Pro