By GokiSoft.com| 22:52 29/10/2021|
Java Advanced

[Video] Phân chia mảng số nguyên thành 2 phần + chắc + lẻ

Phân chia mảng số nguyên thành 2 phần + chắc + lẻ


Source Code


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exam;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N = ");
        int N = scan.nextInt();
        int[] t = new int[N];
        //Nhap du lieu
        for (int i = 0; i < N; i++) {
            System.out.format("\nNhap t[%d] = ", i);
            t[i] = scan.nextInt();
        }
        //Cach 1:
        //Sap xep mang do theo thu tu tang dan
        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;
                }
            }
        }
        //Phan chia mang thanh 2 phan => so chan va so le
        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];
        }
        
        System.out.println("\nKet qua:\n");
        for (int i = 0; i < N; i++) {
            System.out.print(t[i] + ", ");
        }
    }
}


#Main2.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exam;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhap N = ");
        int N = scan.nextInt();
        int[] t = new int[N];
        //Nhap du lieu
        for (int i = 0; i < N; i++) {
            System.out.format("\nNhap t[%d] = ", i);
            t[i] = scan.nextInt();
        }
        
        //Tach du lieu - theo yeu cau de bai
        int index = -1;//insert sort
        //t => 1, 9, 2, 7, 10, 4, 5, 6
        for (int i = 0; i < N; i++) {
            if(t[i] % 2 == 0) {
                //so chan => day ve phia trai cua mang t
                int position = getPositionInsert(t, t[i], 0, (index >= 0)?index:i);
                System.out.println("even position >> " + position);
                if(position < i) {
                    int tmp = t[i];
                    moveElements(t, position, i);
                    t[position] = tmp;
                }
                if(index >= 0) {
                    index++;
                }
            } else {
                //insert phan tu vao dung vi tri cua no
                if(index == -1) {
                    index = i;
                } else {
                    int position = getPositionInsert(t, t[i], index, i);
                    System.out.println("odd position >> " + position);
                    if(position < i) {
                        int tmp = t[i];
                        moveElements(t, position, i);
                        t[position] = tmp;
                    }
                }
            }
        }
        
        System.out.println("\nKet qua: ");
        for (int i = 0; i < N; i++) {
            System.out.print(t[i] + ", ");
        }
    }
    
    static void moveElements(int[] t, int start, int end) {
        for (int i = end; i > start; i--) {
            t[i] = t[i - 1];
        }
    }
    
    static int getPositionInsert(int[] t, int value, int start, int end) {
        for (int i = start; i < end; i++) {
            if(t[i] > value) {
                return i;
            }
        }
        return end;
    }
}


Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)