Bài tập 3 - C# Console

Bài tập 3 - C# Console

Câu 3: Viết chương trình thực hiện các công việc sau
a. Cho phép nhập n số nguyên
b. Xuất mảng số nguyên vừa nhập
c. In 2 dòng: _Các phần tử âm theo thứ tự nhập

_ Các phần tử dương ngược thứ tự nhập
d. Đếm số phần tử âm trong mảng
e. Tính tổng các phần tử ở vi6 trí chẵn
f. In các số hoàn thiện trong mảng
g. Nhập x, in tất cả các vị trí của x trong mảng (nếu co’)
h. Nhập x, thêm x vào đầu mảng
i. Xoá phần tử cuối mảng

Gợi ý giải

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Bai_tap_3

{

class Program

{

public void setArray(ref int n, ref int[] a)

{

do

{

Console.Write("Nhap n (0<n<100): ");

n = int.Parse(Console.ReadLine());

} while (n >= 100 || n <= 0);

for (int i = 0; i < n; i++)

{

Console.Write("Nhap a[{0}]: ", i);

a[i] = int.Parse(Console.ReadLine().ToString());

}

}

//Ham in tat ca cac phan tu trong mang

public void print(int[] a, int n)

{

Console.WriteLine("Day so vua nhap:");

for (int i = 0; i < n; i++)

{

Console.Write("{0}\t", a[i]);

}

}

//Ham kt 1 so phai so hoan thien khong

public Boolean isPerfectNumber(int n)

{

if (n < 0) return false;

int sum = 1;

for (int i = 2; i < n; i++)

{

if (n % i == 0)

{

sum += i;

if (sum > n) return false;

}

}

return sum == n;

}

//Ham them 1 phan tu vao dau mang

public void insertFirst(ref int[] a, ref int n, int x)

{

for (int i = n; i > 0; i--)

{

a[i] = a[i - 1];

}

a[0] = x;

n++;

}

//Ham xoa 1 phan tu cuoi khoi mang

public void removeLast(int[] a, ref int n)

{

if (n == 0) return;

n--;

}

static void Main(string[] args)

{

Program p = new Program();

//a. Cho phep nhap n so nguyen (0<n<100)

int n = 0;

int[] a = new int[100];

p.setArray(ref n, ref a);

//b. Xuat mang so nguyen vua nhap

p.print(a, n);

//c) In 2 dong:

//_ Cac phan am theo thu tu nhap

String tmp = "";

for (int i = 0; i < n; i++)

{

if (a[i] < 0) { tmp += a[i] + "\t"; }

}

if (tmp == "")

{

Console.WriteLine("\nDay so khong co so am!");

}

else

{

Console.WriteLine("\nCac phan tu am theo thu tu nhap: {0}", tmp);

}

//_Cac phan tu duong nguoc thu tu nhap

tmp = "";

for (int i = n - 1; i >= 0; i--)

{

if (a[i] > 0) { tmp += a[i] + "\t"; }

}

if (tmp == "")

{

Console.WriteLine("\nDay so khong co so duong!");

}

else

{

Console.WriteLine("\nCac phan tu duong nguoc thu tu nhap: {0}", tmp);

}

//d. Dem so phan tu am trong mang

int count = 0;

for (int i = 0; i < n; i++)

{

if (a[i] < 0) count++;

}

Console.WriteLine("So phan tu am trong day la: {0}", count);

//e. Tinh tong cac phan tu o vi tri chan

long total = 0;

for (int i = 0; i < n; i+=2)

{

total += a[i];

}

Console.WriteLine("Tong cac phan tu o vi tri chan: {0}", total);

//f. In cac so hoan thien trong mang

tmp = "";

for (int i = 0; i < n; i += 2)

{

if (p.isPerfectNumber(a[i])) { tmp += a[i] + "\t"; }

}

if (tmp == "")

{

Console.WriteLine("\nDay so khong co so hoan thien!");

}

else

{

Console.WriteLine("\nCac so hoan thien trong day: {0}", tmp);

}

//g. Nhap x, in tat ca cac vi tri cua x trong mang (neu co)

int x;

Console.Write("Nhap x: ");

x = int.Parse(Console.ReadLine());

tmp = "";

for (int i = 0; i < n; i++)

{

if (a[i] == x) tmp += i + "\t";

}

if (tmp == "")

{

Console.WriteLine("\nKhong tim thay {0} trong day so!", x);

}

else

{

Console.WriteLine("\nCac vi tri xuat hien {0} trong day so: {1}", x, tmp);

}

//h. Nhap x, them x vao dau mang

Console.Write("Nhap x: ");

x = int.Parse(Console.ReadLine());

p.insertFirst(ref a, ref n, x);

p.print(a, n);

//i. Xoa phan tu cuoi mang

Console.WriteLine("\nXoa phan tu cuoi");

p.removeLast(a, ref n);

p.print(a, n);

//

Console.ReadKey(true);

}

}

}
Bạn thấy bài viết này như thế nào?: 
Average: 9.5 (2 votes)
Ảnh của Binh Tran Thanh

Drupal Consultant

Started my career as a drupal8 developer in EM Solutions . I love learning Web technologies like HTML, CSS, PHP, Jquery Ajax and Drupal backend . Currently working as a drupal backend developer.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Các thao tác cơ sở làm việc mongodb trong java

Các thao tác cơ sở làm việc mongodb trong java

Để  làm việc được với mongodb trước hết bạn download thư viện mongo tại link sau :

5 Videos mới cần thiết để Working với Blocks trong Drupal 8

5 Videos mới cần thiết để Working với Blocks trong Drupal 8

Next week we'll start digging into the State API to get more parts of our block moved over. Have a great week and enjoy!

Giới thiệu chung về Facebook qua số liệu cụ thể

Facebook là một website mạng xã hội truy cập miễn phí do công ty Facebook, Inc 

Công ty diệt chuột T&C

 

Diet con trung