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)
GokiSoft.com
2021-05-19 07:07:01
using System;
namespace BT1395
{
class MainClass
{
public static void Main(string[] args)
{
int N;
Console.WriteLine("Nhap so phan tu cua mang N: ");
N = Int32.Parse(Console.ReadLine());
int[] t = new int[N];
Console.WriteLine("Nhap du lieu cho mang t[N]: ");
for(int i=0;i<N;i++)
{
Console.WriteLine("Nhap t[{0}] = ", i);
t[i] = Int32.Parse(Console.ReadLine());
}
//Sap xep du lieu: Chan phia tren (left), le phai duoi (right)
//t = {1, 5, 2, 7, 6, 9)
//var: currentIndex -> tro toi vi tri index cuoi cung so chan trong mang.
//currentIndex = -1
//i = 2, t[i] = 2 -> so chan -> chuyen t[i] = 2 len vi tri sau currentIndex -> update currentIndex moi.
int currentIndex = -1;
for (int i = 0; i < N; i++)
{
if(t[i] % 2 == 0)
{
currentIndex++;
int tmp = t[i];
t[i] = t[currentIndex];
t[currentIndex] = tmp;
}
}
//co dc 1 mang moi thuc hien xep theo yeu cau
//t = {2, 6, 1, 7, 5, 9}
Console.WriteLine("Ket qua sap xep mang:");
for (int i = 0; i < N; i++)
{
Console.WriteLine("{0}", t[i]);
}
//dem so phan tu xua hien trong mang
//t = {2, 1, 1, 7, 1, 9}
//count = {1, 1, 1, 1, 1, 1}
//kiem tra va don mang count
//count = {1, 3, 0, 1, 0, 1}
int[] count = new int[N];
for(int i=0;i<N;i++)
{
count[i] = 1;
}
for(int i=0;i<N-1;i++)
{
if (count[i] == 0) continue;
for(int j=i+1;j<N;j++)
{
if (count[j] == 0) continue;
if(t[i] == t[j])
{
count[i]++;
count[j]--;
}
}
}
int max = 0;
int index = 0;
for (int i = 0; i < N; i++)
{
if(count[i] > 0)
{
if(max < count[i])
{
max = count[i];
}
index = i;
Console.WriteLine("Phan tu {0} xuat hien {1} lan", t[i], count[i]);
}
}
Console.WriteLine("MAX: Phan tu {0} xuat hien {1} lan", t[index], max);
}
}
}
GokiSoft.com
2021-05-19 06:56:24
using System;
namespace BT1395
{
class MainClass
{
public static void Main(string[] args)
{
int N;
Console.WriteLine("Nhap so phan tu cua mang N: ");
N = Int32.Parse(Console.ReadLine());
int[] t = new int[N];
Console.WriteLine("Nhap du lieu cho mang t[N]: ");
for(int i=0;i<N;i++)
{
Console.WriteLine("Nhap t[{0}] = ", i);
t[i] = Int32.Parse(Console.ReadLine());
}
//Sap xep du lieu: Chan phia tren (left), le phai duoi (right)
//t = {1, 5, 2, 7, 6, 9)
//var: currentIndex -> tro toi vi tri index cuoi cung so chan trong mang.
//currentIndex = -1
//i = 2, t[i] = 2 -> so chan -> chuyen t[i] = 2 len vi tri sau currentIndex -> update currentIndex moi.
int currentIndex = -1;
for (int i = 0; i < N; i++)
{
if(t[i] % 2 == 0)
{
currentIndex++;
int tmp = t[i];
t[i] = t[currentIndex];
t[currentIndex] = tmp;
}
}
//co dc 1 mang moi thuc hien xep theo yeu cau
//t = {2, 6, 1, 7, 5, 9}
Console.WriteLine("Ket qua sap xep mang:");
for (int i = 0; i < N; i++)
{
Console.WriteLine("{0}", t[i]);
}
}
}
}
Do Trung Duc
2021-05-18 04:44:52
using System;
namespace InChanLe
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap so phan tu cua mang N: ");
int N = Int32.Parse(Console.ReadLine());
int[] arr = new int[N];
for (int i = 0; i < N; i++) {
Console.WriteLine("Nhap gia tri phan tu thu {0} ", i +1);
int number = Int32.Parse(Console.ReadLine());
arr[i] = number;
}
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j < N -1; j++)
{
if(arr[j] % 2 == 0) {
int swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
}
Console.WriteLine("Cac phan tu cua mang sap xep lai nhu sau:");
Console.WriteLine("");
for (int i = 0; i < N; i++)
{
Console.WriteLine(arr[i] + "");
}
}
}
}
vuong huu phu
2021-05-14 09:34:49
using System;
namespace bai2
{
class Program
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Nhap so n");
n = Int32.Parse(Console.ReadLine());
int[] a = new int[n];
for (int i = 0; i < n; i++) {
int e;
Console.WriteLine("Nhap phan tu thu " + i);
e = Int32.Parse(Console.ReadLine());
a[i] = e;
}
int t = 0;
for (int i = 0; i < n -1 ; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] % 2 != 0) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
Console.WriteLine("mang da sap xep : " );
for (int i = 0; i < n; i++) {
Console.WriteLine(a[i]);
}
}
}
}
Nguyễn Tiến Đạt
2021-05-14 09:31:23
using System;
namespace ptb1
{
internal class Program
{
private static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
int[] array = new int[n];
var stringArray = Console.ReadLine().Split(' ');
for (int i = 0; i < n; i++)
{
array[i] = int.Parse(stringArray[i]);
}
Array.Sort(array);
for (int i = 0; i < n; i++)
{
if (array[i] % 2 == 0) Console.Write("{0} ", array[i]);
}
for (int i = 0; i < n; i++)
{
if (array[i] % 2 != 0) Console.Write("{0} ", array[i]);
}
}
}
}
Lê Minh Bắc
2020-05-19 03:28:33
using System;
namespace Lession2
{
class Chanle
{
static void Main(string[] args)
{
int N;
Console.Write("Nhap N: ");
N = int.Parse(Console.ReadLine());
int[] array = new int[N];
for (int i = 0; i < N; i++)
{
Console.Write("Nhap phan tu thu {0}: ",i);
array[i] = int.Parse(Console.ReadLine());
}
// Chia mang
int[] ch = new int[N];
int[] le = new int[N];
int c = 0, d = 0;
for (int i = 0; i < N; i++)
{
if (array[i] % 2 == 0)
{
ch[c] = array[i];
c++;
}
else
{
le[d] = array[i];
d++;
}
}
for (int i = 0; i < c; i++)
{
array[i] = ch[i];
}
for (int j = 0; j < d; j++)
{
array[c] = le[j];
c++;
}
Console.Write("Mang sau khi chia: ");
for (int i = 0; i < N; i++)
{
Console.Write(array[i] + " ");
}
Console.ReadKey();
}
}
}
NguyenHuuThanh
2020-05-18 06:07:21
using System;
namespace SeparateStringthenCombine
{
class Program
{
static void Main(string[] args)
{
int N;
int temp = 0;
N = int.Parse(Console.ReadLine());
int[] array = new int[N];
for ( int i = 0; i < N; i++ )
{
array[i] = int.Parse(Console.ReadLine());
}
// Sap xep mang tang dan :
for ( int i = 0; i < N - 1; i++)
{
for ( int j = i + 1; j < N; j++)
{
if ( array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
// Chia mang
int[] t1 = new int[N];
int[] t2 = new int[N];
int numb = 0, numb1 = 0;
for(int i = 0; i < N; i++)
{
if ( array[i] % 2 == 0)
{
t1[numb++] = array[i];
}
else
{
t2[numb1++] = array[i];
}
}
for ( int i = 0; i < numb; i++)
{
array[i] = t1[i];
}
for ( int j = 0; j < numb1; j++)
{
array[numb++] = t2[j];
}
for ( int i = 0; i < N; i++)
{
Console.Write(array[i] + ",");
}
}
}
}
thienphu
2020-05-18 02:04:56
using System;
namespace Lesson2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("nhap N phan tu");
int n = Convert.ToInt32(Console.ReadLine());
int[] mang = new int[n];
for (int i = 0; i < mang.Length; i++)
{
Console.Write("\nNhap mang[{0}] = ",(i));
mang[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
for (int i = 0; i < mang.Length; i++)
{
Console.Write(+mang[i]+"\t");
}
sortNumber(mang);
Console.WriteLine("\n Sau khi sap xep:");
for (int i = 0; i < mang.Length; i++)
{
Console.Write(+mang[i] + "\t");
}
Console.ReadKey();
}
public static void sortNumber(int [] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if ((arr[i] % 2 != 0 && arr[j] % 2 == 0)
|| (arr[i] % 2 == 0 && arr[j] % 2 == 0 && arr[i] > arr[j])
|| (arr[i] % 2 != 0 && arr[j] % 2 != 0 && arr[i] > arr[j]))
{
int index = arr[i];
arr[i] = arr[j];
arr[j] = index;
}
}
}
}
}
}
Đường Thanh Bình
2020-05-17 12:54:22
using System;
using System.Text;
namespace Indaysochanle
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("Nhập số phần tử trong mảng :");
int n = int.Parse(Console.ReadLine());
int[] t = new int[n];
for(int i = 0; i < n; i++)
{
Console.Write("Nhập phần tử thứ {0} :", (i + 1));
t[i] = int.Parse(Console.ReadLine());
}
int[] chan = new int[n];
int[] le = new int[n];
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (t[i] > t[j])
{
int k = t[i];
t[i] = t[j];
t[j] = k;
}
}
}
int i1 = 0, i2 = 0;
for (int i = 0; i < n - 1; i++)
{
if (t[i] % 2 == 0)
{
chan[i1++] = t[i];
}
else
{
le[i2++] = t[i];
}
}
for(int i = 0; i < i1; i++)
{
t[i] = chan[i];
}
for (int i = 0; i < i2; i++)
{
t[i1++] = le[i];
}
for (int i = 0; i < n; i++)
{
Console.Write(t[i] + ",");
}
}
}
}
Trương Công Vinh
2020-05-16 07:29:07
using System;
namespace chanle
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Nhap so phan tu trong mang : ");
int n = int.Parse(Console.ReadLine());
int[] t = new int[n];
for(int i = 0; i <n; i++)
{
Console.Write("nhap phan tu thu {0} : ",(i+1));
t[i] = int.Parse(Console.ReadLine());
}
int[] chan = new int[n];
int[] le = new int[n];
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (t[i] > t[j])
{
int k = t[i];
t[i] = t[j];
t[j] = k;
}
}
}
int i1 = 0,i2=0;
for (int i = 0; i < n - 1; i++)
{
if (t[i] % 2 ==0)
{
chan[i1++] = t[i];
}
else
{
le[i2++] = t[i];
}
}
for(int i = 0; i < i1; i++)
{
t[i] = chan[i];
}
for (int i = 0; i < i2; i++)
{
t[i1++] = le[i];
}
for (int i = 0; i < n; i++)
{
Console.Write(t[i] + " , ");
}
}
}
}