By GokiSoft.com| 15:30 14/06/2023|
Java Basic

Java Basic - Tìm dãy số nguyên tố trong mảng BT1375

Nhập vào mảng số nguyên t gồm N phần tử

int[] t = new int[N];

Yêu cầu:

- Hiển thị các số nguyên tố trong mảng vừa nhập

- Sắp xếp các số nguyên tố theo thứ tự tăng dần.

Sử dụng bằng các phương pháp sau

Phương pháp 1: Làm bằng bất kỳ cách nào các bạn có thể giải quyết

Phương pháp 2: Chỉ được sử dụng duy nhất mảng số nguyên t để sắp xếp.

Liên kết rút gọn:

https://gokisoft.com/1375

Bình luận

avatar
Trần Nhựt Linh [java1_online]
2023-03-31 13:14:16



/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */

package com.mycompany.timdaysont;

import java.util.Scanner;

/**
 *
 * @author mymem
 */
public class TimdaysoNT {
//    // dung 2 mang khac nhau
//     public static boolean checkNT(int n) {
//        if (n <= 1) {
//            return false;
//        }
//
//        for (int i = 2; i <= n / 2; i++) {
//            if (n % i == 0) {
//                return false;
//            }
//        }
//
//        return true;
//    }
//     public static void sort(int[] t,int s) {
//        int temp;
//        for(int i =0;i<(s-1);i++){
//                for(int j=(i+1);j<s;j++){
//                    if(t[i]>t[j]){
//                        temp=t[i];
//                        t[i]=t[j];
//                        t[j]=temp;
//                    }
//                }
//            }
//    }
//
//    public static void main(String[] args) {
//        System.out.println("Nhap vao N:");
//        Scanner sc = new Scanner(System.in);
//        int n = sc.nextInt();
//        int[] t = new int[n];
//        int[] t1 = new int[n];
//        for(int i =0;i<n;i++){
//            System.out.println("Nhap phan tu thu " +(i+1));
//            t[i]=sc.nextInt();
//            
//        }
//        int j=0;
//        for(int i = 0;i<n;i++){
//            if(checkNT(t[i])==true){
//                System.out.print(" "+t[i]);
//                t1[j]=t[i];
//                j++;
//            }
//        }
//        int s=j;
//        sort(t1,s);
//        System.out.println();
//        System.out.println("Day so NT tang dan la:");
//        for(int i = 0;i<j;i++){
//                System.out.print(" "+t1[i]);
//            
//        }
//    }
    // dung 1 mang
     public static boolean checkNT(int n) {
        if (n <= 1) {
            return false;
        }

        for (int i = 2; i <= n / 2; i++) {
            if (n % i == 0) {
                return false;
            }
        }

        return true;
    }
     public static void sort(int[] t,int s) {
        int temp;
        for(int i =0;i<(s-1);i++){
                for(int j=(i+1);j<s;j++){
                    if(t[i]>t[j]){
                        temp=t[i];
                        t[i]=t[j];
                        t[j]=temp;
                    }
                }
            }
    }

    public static void main(String[] args) {
        System.out.println("Nhap vao N:");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] t = new int[n];
        for(int i =0;i<n;i++){
            System.out.println("Nhap phan tu thu " +(i+1));
            t[i]=sc.nextInt();
            
        }
        int j=0;
        for(int i = 0;i<n;i++){
            if(checkNT(t[i])==false){
                
                t[i]=0;
      
            }
            else {System.out.print(" "+t[i]);
            j++;
            }
        }
        

        sort(t,n);
        System.out.println();
        System.out.println("Day so NT tang dan la:");
        for(int i = (n-j);i<n;i++){
                System.out.print(" "+t[i]);
            
        }
    }
}


avatar
le anh tuan [java1_online]
2022-09-07 10:48:28



package array;

import java.util.Scanner;

/**
 *
 * @author Skynet
 */
public class SoNguyenTo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhap so phan tu: ");
        int n = scanner.nextInt();
        int[] t = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.format("Nhap mang t[%d]: ",i);
            t[i] = scanner.nextInt();  
        }
        System.out.print("Cac so nguyen to la: ");
        for (int i = 0; i < t.length; i++) {
            if(isPrimeNumber(t[i])){
                System.out.print(t[i]+" ");
            }
        }
        System.out.println("");
        sortASC(t);
        System.out.print("Day so dc sap xep la: ");
        show(t);
    }
    
    public static boolean isPrimeNumber(int n) {
        // so nguyen n < 2 khong phai la so nguyen to
        if (n < 2) {
            return false;
        }
        // check so nguyen to khi n >= 2
        int squareRoot = (int) Math.sqrt(n);
        for (int i = 2; i <= squareRoot; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }
    // xap xep so nguyen theo thu tu tang dan
    public static void sortASC(int[] t) {
        int temp = t[0];
        for (int i = 0 ; i < t.length - 1; i++) {
            for (int j = i + 1; j < t.length; j++) {
                if (t[i] > t[j]) {
                    temp = t[j];
                    t[j] = t[i];
                    t[i] = temp;
                }
            }
        }
    }
    // in cac phan tu ra man hinh
    public static void show(int[] t) {
        for (int i = 0; i < t.length; i++) {
            System.out.print(t[i] + " ");
        }
    }
}


avatar
Nguyễn Hoàng Anh [C1907L]
2020-05-13 13:14:12



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

import java.util.Scanner;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap N:");
        int N = Integer.parseInt(input.nextLine());
        int[] t = new int[N];
        int[] temp = new int[N];
        for (int i = 0; i < N; i++) {
            System.out.println("Nhap phan thu thu " + (i + 1) + " :");

            t[i] = Integer.parseInt(input.nextLine());

        }
        int k = 0;
        for (int i = 0; i < t.length; i++) {

            Boolean flag = true;

            for (int j = 2; j < t[i]; j++) {

                if (t[i] % j == 0) {
                    flag = false;
                }
            }
            if (flag == true) {
                
               temp[k] = t[i];
            }
            k += 1;
        }
        t = new int[k];
        for(int i = 0 ; i < temp.length; i++){
            if(temp[i] != 0){               
                t[i] = temp[i];
            }
        }         
        for (int i = 0; i < t.length - 1; i++) {
            for (int j = i + 1; j < t.length ; j++) {
                if (t[i] > t[j]) {
                    int te;
                    te = t[i];
                    t[i] = t[j];
                    t[j] = te;
                }
            }
        }
        for (int i = 0; i < t.length; i++) {
            if(t[i] != 0){
                System.out.println(t[i]);
            }           
        }
    }
}


avatar
Nguyễn Hữu Đạt [C1907L]
2020-05-13 13:05:36



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

import java.util.Scanner;

/**
 *
 * @author exam4
 */
public class Main {
   public static void main(String[] args) {
       int n, min;
       Scanner scanner = new Scanner(System.in);
       System.out.println("Nhập vào số phần tử của mảng: ");
       n = scanner.nextInt();
       int [] t = new int[n];
       for (int i = 0; i < n; i++) {
        System.out.print("Nhập phần tử thứ " + (i + 1) + ": ");
        t[i] = scanner.nextInt();
        }
       System.out.println("\nCác số nguyên tố trong mảng vừa nhập là: ");
        for (int i = 0; i < n; i++) {
            if(checkNguyenTo(t[i])){
            System.out.print(t[i] + "\t");
            }
        }
        for(int i = 0; i < n - 1; i++){
            for(int j = i + 1; j <= n-1; j++){
                if(t[i] > t[j]){
                    min = t[i];
                    t[i] = t[j];
                    t[j] = min;
                }
            }
        }
        System.out.println("\nCác số nguyen tố sắp xếp tăng dần là: ");
        for (int i = 0; i < n; i++) {
            if(checkNguyenTo(t[i])){
            System.out.print(t[i] + "\t");
            }
        }
        
    }
   public static boolean checkNguyenTo(int n){
            if(n<=2){
             return true;
            }else {
             for(int i =2;i<=Math.sqrt(n);i++){
              if(n % i == 0)
               return false;
             }
            }
            return true;
           }
    
}


avatar
hoangkhiem [C1907L]
2020-05-13 12:53:34



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

import java.util.Scanner;

/**
 *
 * @author Admin
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N , tmp;
        System.out.println("Nhap phan tu N : ");
        N = Integer.parseInt(scan.nextLine());
        int[] t = new int[N];
        for (int i = 0; i < t.length; i++) {
            System.out.format("arr[%d]= ", i+1);
            t[i] = Integer.parseInt(scan.nextLine());
        }
        System.out.println("Các số nguyên tố là : ");
        for (int i : t) {
            if (isPrime(i)) {
                System.out.format("[%d]",i);
            }
        }
        System.out.println("*********************");
       for(int i = 0; i < t.length - 1; i++){
        for(int j = i + 1; j < t.length; j++){
            if(t[i] > t[j]){
                // Hoan vi 2 so a[i] va a[j]
                tmp = t[i];
                t[i] = t[j];
                t[j] = tmp;        
            }
        }
    }for (int i : t) {
            System.out.format("[%d]",i);
        }
    }
    public static boolean isPrime(int n) {
       if (n <= 1) {
           return false;
       }
       for (int i = 2; i <= Math.sqrt(n); i++) {
           if (n % i == 0) {
               return false;
           }
       }
       return true;
   }
}


avatar
Nguyen Trinh Thanh Thuy [community]
2020-05-13 12:50:27



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

import static java.lang.Integer.parseInt;
import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author student
 */
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("nhap mang (phan chia boi space): ");

        String to_arr = scan.nextLine();
        int[] nto
                = new int[0];
        String[] arr = to_arr.split("\\s");
        int count = 0;
        System.out.println(Arrays.toString(arr));
        for (String temp : arr) {
            
            int[] ptu_temp
                    = new int[count + 1];
            System.arraycopy(nto, 0, ptu_temp, 0, count);

            ptu_temp[count] = parseInt(temp);

            int i = 2;
            if (ptu_temp[count] > 2) {
                while ((i<=(int)Math.sqrt(ptu_temp[count]))&&((double)ptu_temp[count]%i!=0)) {
                    i++;
                }
                if (i>Math.floor(Math.sqrt(ptu_temp[count]))){
                    nto = ptu_temp;
                    count++;
                }
            }else{
//                nto = ptu_temp;
//                count++;
            }
//            count++;
        }
        
        System.out.println(Arrays.toString(nto));

    }
}


avatar
Hoàng Quang Huy [C1907L]
2020-05-13 12:27:57




package Review_13_5_2020;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int N;
        Scanner scan = new Scanner(System.in);
        System.out.println("Nhập số lượng phần tử");
        N = Integer.parseInt(scan.nextLine());
        int[] t = new int[N];
        for (int i = 0; i < t.length; i++) {
            System.out.println("Phần tử thứ " + (i+1));
            t[i] = Integer.parseInt(scan.nextLine());
        }
        System.out.println("Dãy số nguyên tố: ");
        for (int i = 0; i < t.length; i++) {
            if (check(t[i])) {
                System.out.print(t[i] + " ");
            }
        }
        System.out.println("");
        System.out.println("Sắp xếp: ");
        int index = 0;
        for (int i = 0; i < t.length; i++) {
            if (check(t[i])) {
                swap(t, i, index);
                index++;
            }
        }
        for (int i = 0; i < index-1; i++) {
            for (int j = i+1; j < index; j++) {
                if(t[i]>t[j]){
                    swap(t, i, j);
                }
            }   
        }
        for (int i = 0; i < index; i++) {
            System.out.print(t[i]+ " ");
        }
    }

    public static boolean check(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}


avatar
Đinh Vũ Anh [C1907L]
2020-05-13 12:22:53



package JavaBasic;
import java.util.Scanner;
import java.util.logging.Logger;
/**
 *
 * @author student
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Nhập số phần tử trong mảng: ");
        int N;
        N = scan.nextInt();
        int[] t = new int[N];
        for(int i = 0; i < N; i++){
            System.out.print("Nhập số: ");
            t[i] = scan.nextInt();
        }
        System.out.println("Các số nguyên tố: ");
        int count;
        for(int i = 0; i < N ; i++) {
            count = 0;
            for(int a = 2; a <= t[i]; a++) {
                if(t[i] % a == 0) {
                    count += 1;
                }
            }
            if(count == 1) {
                System.out.println(t[i]);
            }
        }
        for(int i = 0; i < N - 1; i++){
            for(int j = 1; j < N; j++){
                if(t[i] > t[j]){
                    int x = t[i];
                    t[i] = t[j];
                    t[j] = x;
                }
            }
        }
        System.out.println("Sắp xếp: ");
        for(int i = 0; i < N; i++){
            System.out.println(t[i] +  " ");
        }
    }
    
}


avatar
Ngô Quang Huy [C1907L]
2020-05-13 12:06:35



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

import java.util.Scanner;

/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
        int n;
        Scanner input = new Scanner(System.in);
        System.out.print("Insert n = ");
        n= input.nextInt();
        int[] array = new int[n];
        for(int i=0;i<n;i++){
            System.out.println("Insert: ");
            array[i] = input.nextInt();
        }
        System.out.println("So Nguyen To: ");
        int count=0;
        for (int i = 0; i < n; i++) {
            if(check(array[i])==1){
                System.out.print(array[i]+" ");
            }
        }
        System.out.println("\nSap xep: ");
        for (int i = 0; i < n; i++) {
            if(check(array[i])==1){
                swap(array, i, count);
                count++;
            }
        }
        for(int i=0;i<count-1;i++){
            for (int j = i+1; j < count; j++) {
                if(array[i]>array[j]){
                    swap(array, i, j);
                }
            }
            
        }
        for (int i = 0; i < count; i++) {
            System.out.print(array[i]+" ");
        }
    }
    
    static void swap(int[] array, int i,int j){
                    int tmp= array[i];
                    array[i] = array[j];
                    array[j] = tmp;
    }
    
    static int check(int numb){
        int dk=1;
        for(int i=2;i<numb;i++){
            if(numb%i==0){
                dk=0;
            }
        }
        if(numb==1){
            dk=0;
        }
        return dk;
    }
}


avatar
trung [C1907L]
2020-05-13 11:57:42



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

import java.util.Scanner;

/**
 *
 * @author student
 */
public class Main {
    public static void main(String[] args) {
        int N;
        Scanner inp = new Scanner(System.in);
        N = Integer.parseInt(inp.nextLine());
        int[] t = new int[N];
        for (int i = 0; i < N ; i++) {
            System.out.println("Nhap vao so nguyen thu "+(i+1));
            t[i] = Integer.parseInt(inp.nextLine());
        }
        for (int i = 0; i < N; i++) {
            if (checkPrime(t[i])) System.out.println(t[i]);
        }
        int lastPos = 0;
        int numPrime = 0;
        for (int i = 0; i < N; i++) {
            if (checkPrime(t[i])) {
                swap(t, i, lastPos);
                lastPos++;
                numPrime++;
            }
        }
        for (int i = 0; i < numPrime-1; i++) {
            for (int j = i+1; j < numPrime; j++) {
                if (t[i]>t[j]) {
                    swap(t,i,j);
                }
            }
        }
        for (int i = 0; i < N ; i++) {
            System.out.println(t[i]);
        }
        
    }
    
    private static boolean checkPrime(int n) {
        if (n == 0 || n == 1) {
            return false;
        }
        for (int i = 2; i < n-1; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
    
    private static void swap(int[] m, int n, int p) {
        try {
            int temp = m[p];
            m[p] = m[n];
            m[n] = temp;
        } catch (Exception e) {
            System.out.println("???");
        }
    }
}