Thứ Ba, 10 tháng 7, 2018

(C#) Bài tập 11-A: Tạo Thư viện một số hàm xử lý số đơn giản

Tao class ThuVienHam để chứa các hàm cần xử lí (FILE ThuVienHam.cs)

using System;
class ThuVienHam
{
//HAM KIEM TRA SO NGUYEN TO
    public static bool SoNT(int n)
    {
        if (n < 2)
            return false;
        for (int i = 2; i <= n / 2; i++)
            if (n % i == 0)
                return  false;
        return true;
    }
//HAM KIEM TRA SO HOAN HAO
    public static bool SoHH(int n)
    {
        int tong = 0;
        for (int i = 1; i <= n / 2; i++)
            if (n % i == 0)
                tong=tong+i;
        if (tong == n)
            return true;
        return false;
    }
//HAM TIM UCLN CUA 2 SO
    public static int UCLN(int a,int b)
    {
        while (a!=b)
        {
            if (a > b)
                a = a - b;
            else
                b = b - a;
        }         
        return a;
    }
//HAM TIM BCNN CUA 2 SO
    public static int BCNN(int a, int b)
    {
        return a*b/UCLN(a, b);
    }
//HAM NHAP MANG 1 CHIEU
    public static void nhapMang(int []a,int n)
    {
        for (int i = 0; i < a.Length; i++)
        {
            Console.Write("Phan tu thu [" + i + "]: ");
            a[i] = int.Parse(Console.ReadLine());
        }
    }
//HAM XUAT MANG 1 CHIEU
    public static void inMang(int[] a)
    {
        for (int i = 0; i < a.Length; i++)
            Console.Write(a[i]+"  ");
    }
//HAM HOAN VI 2 SO
    public static void doicho(ref int m, ref int n)
    {
        int tam = m;
        m = n;
        n = tam;
    }
//HAM SAP XEP MANG TANG DAN
    public static void sapxepMang(int[] a)
    {
        for ( int i = 0; i < a.Length; i++)
            for ( int j = i + 1; j < a.Length; j++)
                if (a[i] > a[j])
                doicho(ref a[i],ref a[j]);
        for (int i = 0; i < a.Length; i++)
            Console.Write(a[i]+"  ");
    }
//HAM KIEM TRA SO CHINH PHUONG
    public static bool soChinhPhuong(double n)
    {
        if (n < 4)
            return false;
        int a = (int) Math.Sqrt(n);
        if (a * a == n)
            return true;
        return false;
    }
// VIET TIEP CAC HAM KHAC vao day
// ............
//............
}


(Có) Không có nhận xét nào cho bài viết này

Đăng nhận xét

Hãy nhập Nhận xét cho bài viết này vào khung bên dưới. Cảm ơn...