By GokiSoft.com| 15:32 12/07/2023|
Java Advanced

Tìm mảng a2 kết hợp mảng a1 tạo mảng a là các phân tử liên tiếp nhau

Viết chương trình như sau

- Nhập vào 1 mảng a1 (ví dụ các phần tư trong mảng 6, 2, 4, 8)

Yêu cầu tìm tất cả các phân tử trong mảng a2 sao cho. Khi kết hợp mảng a1 vs mảng a2 chúng ta được 1 mảng a là một dãy số nguyên liên tiếp

Ví dụ mảng a1 là 6, 2, 4, 8

Mảng a2 cần tìm sẽ là: 3, 5, 7

Mảng a kết hợp sẽ là: 2, 3, 4, 5, 6, 7, 8

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

5

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

Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-05-08 06:23:43



/*
 * 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 test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Redmibook 14
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    public static void sort(List<Integer> a) {
        for (int i = 0; i < a.size(); i++) {
            for (int j = i + 1; j < a.size(); j++) {
                if (a.get(i) > a.get(j)) {
                    int t;
                    t = a.get(i);
                    a.set(i, a.get(j));
                    a.set(j, t);
                }
            }
        }
    }

    public static int[] merge(List<Integer> a, List<Integer> b) {

        int lastIndexArra = a.size() - 1;
        int lastIndexArrb = b.size() - 1;
        int lastIndexArrMerge = a.size() + b.size() - 1;
        int[] ArrM = new int[lastIndexArrMerge + 1];
        for (int i = lastIndexArrMerge; i > -1; i--) {
            if (lastIndexArra > -1 && lastIndexArrb > - 1) {
                if (a.get(lastIndexArra) > b.get(lastIndexArrb)) {
                    ArrM[i] = a.get(lastIndexArra--);
                } else {
                    ArrM[i] = b.get(lastIndexArrb--);
                }
            } else if (lastIndexArrb == - 1) {
                ArrM[i] = a.get(lastIndexArra--);
            } else if (lastIndexArra == - 1) {
                ArrM[i] = b.get(lastIndexArrb--);
            }

        }
        return ArrM;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        while (true) {
            System.out.println("Enter fist int array ");
            System.out.println("Number " + (list.size() + 1) + " :");
            int num1 = Integer.parseInt(input.nextLine());
            list.add(num1);
            System.out.println("Continue? (N/Y)");
            String y = input.nextLine();
            if (y.equals("N") || y.equals("n")) {
                break;
            }
        }

        sort(list);

        for (int i = list.get(0); i <= list.get(list.size() - 1); i++) {
            Boolean flag = true;
            for (int j = 0; j < list.size(); j++) {
                //System.out.print(list.get(j));
                if (i == list.get(j)) {
                    flag = false;
                }
            }
            if (flag == true) {
                list2.add(i);
            }
        }
        sort(list2);
        System.out.print("Merger 2 array low to high : ");
        for (int i = 0; i < merge(list, list2).length; i++) {
            System.out.print(merge(list, list2)[i]);
        }
    }
}



Ngô Quang Huy [C1907L]
Ngô Quang Huy

2020-04-25 02:57:17



/*
 * 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 April25ArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Administrator
 */
public class Main {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        System.out.println("\n\tList number 1:");
        input(list1);
        System.out.println("\n\tList number 2:");
        input(list2);
        List<Integer> newList = new ArrayList<>();
        newList.addAll(list1);
        newList.addAll(list2);
        for(int i=0;i<newList.size()-1;i++){
            for(int j=i+1;j<newList.size();j++){
                if(newList.get(i)>newList.get(j)){
                    Collections.swap(newList, i, j);
                }
            }
        }
        System.out.print("==> The Merge List: ");
        for (Integer integer : newList) {
            System.out.print(integer+" ");
        }
    }
    static void input(List list){
        while(true){
            Scanner input = new Scanner(System.in);
            System.out.print("Insert number: ");
            int a= Integer.parseInt(input.nextLine());
            list.add(a);
            System.out.print("Continue: y/n: ");
            String b=input.nextLine();
            if("n".equals(b)){
                break;
            }
        }
    }
}



Nguyễn Hoàng Anh [C1907L]
Nguyễn Hoàng Anh

2020-04-24 15:12:30



/*
 * 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 mergerstringapril24;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author Redmibook 14
 */
public class MergerStringApril24 {

    /**
     * @param args the command line arguments
     */
    public static void sort(List<Integer> a) {
        for (int i = 0; i < a.size(); i++) {
            for (int j = i + 1; j < a.size(); j++) {
                if (a.get(i) > a.get(j)) {
                    int t;
                    t = a.get(i);
                    a.set(i, a.get(j));
                    a.set(j, t);
                }
            }
        }
    }

    public static int[] merge(List<Integer> a, List<Integer> b) {
        sort(a);
        sort(b);
        int lastIndexArra = a.size() - 1;
        int lastIndexArrb = b.size() - 1;
        int lastIndexArrMerge = a.size() + b.size() - 1;
        int[] ArrM = new int[lastIndexArrMerge + 1];
        for (int i = lastIndexArrMerge; i > -1; i--) {
            if (lastIndexArra > -1 && lastIndexArrb > - 1) {
                if (a.get(lastIndexArra) > b.get(lastIndexArrb)) {
                    ArrM[i] = a.get(lastIndexArra--);
                } else {
                    ArrM[i] = b.get(lastIndexArrb--);
                }
            } else if (lastIndexArrb == - 1) {
                ArrM[i] = a.get(lastIndexArra--);
            } else if (lastIndexArra == - 1) {
                ArrM[i] = b.get(lastIndexArrb--);
            }

        }
        return ArrM;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        while (true) {
            System.out.println("Enter fist int array ");
            System.out.println("Number " + (list.size() + 1) + " :");
            int num1 = Integer.parseInt(input.nextLine());
            list.add(num1);
            System.out.println("Continue? (N/Y)");
            String y = input.nextLine();
            if (y.equals("N") || y.equals("n")) {
                break;
            }
        }
        while (true) {
            System.out.println("Enter second int array ");
            System.out.println("Number " + (list2.size() + 1) + " :");
            int num1 = Integer.parseInt(input.nextLine());
            list2.add(num1);
            System.out.println("Continue? (N/Y)");
            String y = input.nextLine();
            if (y.equals("N") || y.equals("n")) {
                break;
            }
        }
         System.out.print("Merger 2 array low to high : ");
        for (int i = 0; i < merge(list, list2).length; i++) {
            System.out.print(merge(list, list2)[i]);
        }
    }

}



Vũ Việt Đức [C1907L]
Vũ Việt Đức

2020-04-24 14:23:25



/*
 * 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 lession2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author ADMIN
 */
public class Bai1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Integer> a2 = new ArrayList<>();

        int[] a1;
        int max, min;
        
        System.out.print("Nhập số phần tử cho mảng: ");
        int n = Integer.parseInt(scan.nextLine());
        a1 = new int[n];
        
        System.out.println("Nhập danh sách phần tử cho mảng: ");
        for(int i = 0; i < a1.length; i++) {
            System.out.print("Phần tử thứ " + (i + 1) + ": ");
            a1[i] = Integer.parseInt(scan.nextLine());
        }
        
        min = a1[0];
        max = a1[0];

        for(int i = 1; i < a1.length; i++) {
            if(min < a1[i]){
                min = a1[i];
            }
            if(max > a1[i]){
                max = a1[i];
            }
        }
        
        for(int i = min; i < max; i++){
            int count = 0;
            for(int j = 0; j < a1.length; j++){
                if(i == a1[j]){
                    count++;
                    break;
                }
            }
            if(count == 0) {
                a2.add(i);
            }
        }
        
        System.out.println("Danh sách các phần tử mảng a2: ");
        for(int i = 0; i < a2.size(); i++) {
            System.out.println(a2.get(i));
        }
    }
}



trung [C1907L]
trung

2020-04-24 14:06:48



/*
 * 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 jv2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class lession_1 {
    
    public static void main(String[] args) {
        Scanner inp = new Scanner(System.in);
        List<Integer> lst = new ArrayList<>();
        boolean has_next = true;
        while (true) {
            System.out.println("Nhap vao so nguyen");
            lst.add(Integer.parseInt(inp.nextLine()));
            System.out.println("Nhap tiep (y/n) ? ");
            if (inp.nextLine().equalsIgnoreCase("n")) break;
        }
        for (int i = 0; i < lst.size()-1; i++) {
            for (int j = i+1; j < lst.size(); j++) {
                if (lst.get(i) > lst.get(j)) {
                    int tmp = lst.get(j);
                    lst.set(j, lst.get(i));
                    lst.set(i,tmp);
                }
            }
        }
        List<Integer> lst2 = new ArrayList<>();
        for (int i = 0; i< lst.size()-1; i++) {
            for (int j = lst.get(i)+1; j < lst.get(i+1); j++) {
                lst2.add(j);
            }
        }
        lst2.forEach((v) -> {
            System.out.println(">>>>"+v);
        });
    }
}