By GokiSoft.com|
15:16 14/05/2021|
C Sharp
In dãy số chẵn & lẻ từ mảng số nguyên - Lập trình C# - Loop trong C#
Nhập vào 1 mảng số nguyên gồm N phần tử. Thực hiện tạo 1 mảng chứa các phẩn tử đầu là số chẵn, các phần tử sau là số lẻ
Ví dụ : mảng đầu vào >> 1, 5, 2, 7, 6, 9
Mảng đầu ra >> 2, 6, 1, 5, 7, 9
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
nguyễn văn huy
2020-05-16 05:16:19
using System;
namespace chanle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("nhập :");
int n = Convert.ToInt32(Console.ReadLine());
int[] m = new int[n];
for (int i = 0; i < n; i++)
{
Console.WriteLine("\nNhap cac so mang m[]" + (i + 1) + ":");
m[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (m[i] > m[j])
{
int tmp = m[i];
m[i] = m[j];
m[j] = tmp;
}
}
}
int[] m1 = new int[n];
int[] m2 = new int[n];
int i1 = 0;
int i2 = 0;
for (int i = 0; i < n; i++)
{
if (m[i] % 2 == 0)
{
m1[i1++] = m[i];
}
else
{
m2[i2++] = m[i];
}
}
for (int i = 0; i < i1; i++)
{
m[i] = m1[i];
}
for (int i = 0; i < i2; i++)
{
m[i1++] = m2[i];
}
Console.WriteLine("\nResult:\n");
for (int i = 0; i < n; i++)
{
Console.WriteLine(m[i] + ", ");
}
}
}
}
hoangduyminh
2020-05-16 00:08:00
using System;
namespace Lession1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Nhap vao N = ");
int N = Convert.ToInt32(Console.ReadLine());
int[] t = new int[N];
for(int i = 0;i < N; i++)
{
Console.WriteLine("\nNhap t[%d]", +i);
t[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i = 0; i < N - 1; i++)
{
for(int j = i+1; j < N; j++)
{
if (t[i] > t[j])
{
int tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
int[] t1 = new int[N];
int[] t2 = new int[N];
int i1 = 0;
int i2 = 0;
for(int i = 0; i < N; i++)
{
if(t[i] % 2 == 0)
{
t1[i1++] = t[i];
}
else
{
t2[i2++] = t[i];
}
}
for(int i = 0; i < i1; i++)
{
t[i] = t1[i];
}
for(int i = 0; i < i2; i++)
{
t[i1++] = t2[i];
}
Console.WriteLine("\nKet Qua:\n");
for (int i = 0; i < N; i++)
{
Console.WriteLine(t[i] + "");
}
}
}
}
Minh Nghia
2020-05-15 13:47:59
using System;
namespace SortChanLe
{
class ChanLe
{
static void Main(string[] args)
{
Console.WriteLine("Enter N :");
int n = Convert.ToInt32(Console.ReadLine());
int[] m = new int[n];
for(int i = 0; i < n; i++)
{
Console.WriteLine("\nNhap cac phan tu mang m[]"+(i + 1 )+":");
m[i] = Convert.ToInt32(Console.ReadLine());
}
//sort
for(int i = 0; i < n-1; i++)
{
for(int j = i + 1; j < n; j++)
{
if(m[i] > m[j])
{
int tmp = m[i];
m[i] = m[j];
m[j] = tmp;
}
}
}
//phan chia mang
int[] m1 = new int[n];
int[] m2 = new int[n];
int i1 = 0;
int i2 = 0;
for(int i = 0; i < n; i++)
{
if(m[i] %2 == 0)
{
m1[i1++] = m[i];
}
else
{
m2[i2++] = m[i];
}
}
for(int i = 0; i < i1; i++)
{
m[i] = m1[i];
}
for(int i = 0; i < i2; i++)
{
m[i1++] = m2[i];
}
Console.WriteLine("\nResult:\n");
for(int i = 0; i < n; i++)
{
Console.WriteLine(m[i] + ", ");
}
}
}
}
Trần Mạnh Dũng
2020-05-15 10:15:07
using System;
namespace sapxepsochanle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of elements in the array:: ");
int n = Convert.ToInt32(Console.ReadLine());
int[] t = new int[n];
for(int i= 0; i < n; i++)
{
Console.WriteLine("\nEnter the element " + (i+1)+":");
t[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i = 0; i < n - 1 ; i++)
{
for(int j = i + 1; j< n; j ++)
{
if (t[i] > t[j])
{
int tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
int[] t1 = new int[n];
int[] t2 = new int[n];
int i1 = 0, i2 = 0;
for (int i = 0; i < n; i++)
{
if (t[i] % 2 == 0)
{
t1[i1++] = t[i];
}
else
{
t2[i2++] = t[i];
}
}
for (int i = 0; i < i1; i++)
{
t[i] = t1[i];
}
for (int i = 0; i < i2; i++)
{
t[i1++] = t2[i];
}
Console.WriteLine("\nResult: ");
for(int i = 0; i < n; i++)
{
Console.WriteLine(t[i] + ", ");
}
}
}
}
Đỗ Văn Huấn
2020-05-15 09:58:36
using System;
namespace vidu1
{
public class Insochanle
{
static void Main(string[] args)
{
int n;
Console.Write("nhap vao so phan tu cua mang: ");
n = int.Parse(Console.ReadLine());
int [] a = new int[n];
for (int i = 0; i < n; i++)
{
Console.Write("nhap phan tu thu "+ (i+1)+ " :");
a[i] = int.Parse(Console.ReadLine());
}
int number;
for (int i =0; i < n -1; i++)
{
for (int j = i+ 1; j < n; j++)
{
if (a[i]> a[j])
{
number = a[i];
a[j] = number;
a[i] = a[j];
}
}
}
int[] b = new int[n];
int [] c = new int[n];
int x = 0, y = 0;
for (int i = 0; i < n; i++)
{
if (a[i] %2 ==0)
{
b[x] = a[i];
x++;
}
else
{
c[y] = a[i];
y++;
}
}
;
for (int i = 0; i < x; i++)
{
a[i] = b[i];
}
for (int i = 0; i < y; i++)
{
a[x++] = c[i];
}
Console.WriteLine("Hien thi day: ");
for (int i = 0; i < n; i++)
{
Console.Write(a[i]+ ", ");
}
}
}
}