Xử lý mảng 1 chiều trong Java

MẢNG 1 CHIỀU:

Dưới đây là 1 vài giải thuật xử lý mảng như đảo mảng, xóa phần tử lẻ, tìm giá trị cực đại.

import java.util.Collections;
import java.util.Random;
import java.util.Scanner;

 
public class Mang_VongLap_DieuKien {
    static Scanner sc = new Scanner(System.in);
    
    
    static int nhapPhanTu(){
         System.out.println("Nhap so luong phan tu: ");
         int n = sc.nextInt();
         return n;
    }
    
    static void nhapMang(int a[], int n){
        for (int i = 0; i < n; i++) {
            System.out.println("Nhap a[" + i + "]");
            a[i] = sc.nextInt();
        }
    }
    
    static void nhapMang_RanDom(int a[], int n){
        Random rd = new Random();
        for (int i = 0; i < n; i++) {
            a[i] = rd.nextInt(100);
        }
    }
     
    static void xuatMang(int a[], int n){
        for (int i = 0; i < n; i++) {
             System.out.println("a[" + i + "]: " + a[i]);
        }
    }
    
     
    static void xuatMang(int a[], int n){
        for (int i = 0; i < n; i++) {
             System.out.println("a[" + i + "]: " + a[i]);
        }
    }
    
    static int tongGiaTriMang(int a[], int n){
        int sum  = 0;
        for (int i = 0; i < n; i++) {
            sum += a[i];
        }
        return sum;
    }
    
    static boolean kiemTraChanLe(int x){
        return x % 2 == 0 ? true : false;
    } 
    
    static void xuatPhanTuGiaTriChan(int a[], int n){
        System.out.println("Cac phan tu gia tri chan: ");
        for (int i = 0; i < n; i++) {
            if(kiemTraChanLe(a[i])) System.out.println(a[i] +" ");
        }   
    }
    
    static void daoNguocMang(int a[], int n){
        System.out.println("Mang sau khi chuyen: ");
        int left = 0, right = n-1;
        while(left < right){
            int tmp = a[left];
            a[left] = a[right];
            a[right] = tmp;
            
            left++;
            right--;
        }
    }
    
     static int xoaPhanTuViTriX(int a[], int n, int x){
         for (int i = x; i < n; i++) {
             a[i] = a[i+1];
         }
         n--;
         return n;
     }
     static int xoaCacPhanTuLeTrongMang(int a[], int n){
         for (int i = 0; i < n; i++) {
             if(!kiemTraChanLe(a[i])) n = xoaPhanTuViTriX(a, n, i);
         }
         return n;
     }
     
    static int timMaxTrongKhoang(int a[], int start, int end) {
	       int max = start;
	       for (int i = start + 1; i <= end; i++){
		         if (a[max] < a[i]) max = i;
	       }
	       return max;
     }
     
    static int giaTriCucDai(int a[], int n){
          if (n < 3) {
            System.out.println("Khong co gia tri cuc tieu");
            return 0;
	        }
         int cucdai = -1, start = -1, end = -1;
         for (int i = 0; i < n; i++) {
            if(a[i] < a[i+1]){
                start  = 0;
                while (a[i] < a[i+1] && i < n){
                    if(i == n) break;
                    i++;
                }
            }
            if(a[i] > a[i+1]){
                end = i;
            }
         }
         return end != -1 ? cucdai  = timMaxTrongKhoang(a, start, end): -1 ;
    }
     
     
    static void list_menu(){
        System.out.println("************************************************************");
        System.out.println("*                               MENU                       *");
        System.out.println("************************************************************");
        System.out.println("* 0. THOAT                                                 *");
        System.out.println("* 1. NHAP VA XUAT MANG                                     *");
        System.out.println("* 2. TAO VA XUAT MANG NGAU NHIEN                           *");
        System.out.println("* 3. TONG GIA TRI MANG                                     *");
        System.out.println("* 4. TIM CAC PHAN TU CO GIA TRI CHAN                       *");
        System.out.println("* 5. DAO NGUOC MANG                                        *");
        System.out.println("* 6. XOA CAC PHAN TU LE TRONG MANG                         *");
        System.out.println("* 7. TIM GIA TRI CUC DAI TRONG MANG                        *");
        System.out.println("*************************************************************");
        
    }
    static void menu(){
        int chon,n = 0, a[] = new int [100];
        do {
            list_menu(); 
            System.out.println("Nhap chuc nang: ");
            chon = sc.nextInt();
            try {
                switch (chon) {
                case 1 -> {
                    n = nhapPhanTu();
                    nhapMang(a, n);
                    xuatMang(a, n);
                }
                case 2 -> {
                    n = nhapPhanTu();
                    nhapMang_RanDom(a, n);
                    xuatMang(a, n);
                }
                case 3 -> {
                    System.out.println("Tong gia tri trong mang la: " + tongGiaTriMang(a, n));
                }
                case 4 -> {
                    xuatPhanTuGiaTriChan(a, n);
                }
                case 5 -> {
                    daoNguocMang(a, n);
                    xuatMang(a, n);
                }
                case 6 -> {
                    n  = xoaCacPhanTuLeTrongMang(a, n);
                    xuatMang(a, n);
                }
                case 7 -> {
                    int cucdai  = giaTriCucDai(a, n);
                    if(cucdai != -1){
                        System.out.println("Gia tri cuc dai trong mang la " + a[cucdai]);
                    }else System.out.println("Khong ton tai gia tri cuc dai");
                }
                default -> throw new AssertionError();
                }
            
            } catch (AssertionError e) {
                System.err.println("Chuc nang khong hop le");
            }
        } while (chon != 0);
    }
    
    
    public static void main(String[] args) {
        menu();
    }
}

Đăng nhận xét

0 Nhận xét