Sắp xếp các phần tử trong mảng tăng dần

 Sắp xếp các phần tử trong mảng tăng dần


public class SapXepPhanTuTangDan
{
    public static void main(String[] args)
    {
        int count, temp;
        Scanner scan = new Scanner(System.in);
        System.out.print("Nhập vào số phần tử trong mảng: ");
        count = scan.nextInt();
        int num[] = new int[count];
        System.out.println("Các phần tử trong mảng là:");
        for (int i = 0; i < count; i++)
        {
            num[i] = scan.nextInt();
        }
        scan.close();
        for (int i = 0; i < count; i++)
        {
            for (int j = i + 1; j < count; j++) {
                if (num[i] > num[j])
                {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }
        System.out.print("Kết quả sau khi được sắp xếp: ");
        for (int i = 0; i < count - 1; i++)
        {
            System.out.print(num[i] + ", ");
        }
        System.out.print(num[count - 1]);
    }
}


Đảo ngược các phần tử trong mảng

 Đảo ngược các phần tử trong mảng


public class DaoNguocPhanTuTrongMang
{
    public static void main(String args[])
    {
        int counter, i=0, j=0, temp;
        int number[] = new int[100];
        Scanner scanner = new Scanner(System.in);
        System.out.print("Nhập vào số phần tử trong mảng: ");
        counter = scanner.nextInt();
        for(i=0; i<counter; i++)
        {
            System.out.print("Phần tử "+(i+1)+": ");
            number[i] = scanner.nextInt();
        }
        j = i - 1;
        i = 0;
        scanner.close();
        while(i<j)
        {
            temp = number[i];
            number[i] = number[j];
            number[j] = temp;
            i++;
            j--;
        }
        System.out.print("Mảng sau khi đảo ngược: ");
        for(i=0; i<counter; i++)
        {
            System.out.print(number[i]+ "  ");
        }
    }
}


Tính trung bình cộng các số trong mảng

 Tính trung bình cộng các số trong mảng



public class TinhTrungBinhCong {
    public static void main(String[] args) {
        System.out.println("Nhập vào số phần tử trong mảng: ");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        double[] arr = new double[n];
        double total = 0;
        for(int i=0; i<arr.length; i++){
            System.out.print("Nhập vào giá trị cho phần tử thứ "+(i+1)+": ");
            arr[i] = scanner.nextDouble();
        }
        scanner.close();
        for(int i=0; i<arr.length; i++){
            total = total + arr[i];
        }
        double average = total / arr.length;
        System.out.format("Kết quả là: %.3f", average);
    }
}

Tìm số nhỏ thứ hai trong mảng

 Tìm số nhỏ thứ hai trong mảng



public class SecondSmallestInArray {
 
    public static int getSecondSmallest(int[] a, int total) {
        int temp;
        for (int i = 0; i < total; i++) {
            for (int j = i + 1; j < total; j++) {
                if (a[i] > a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        return a[1];
    }
 
    public static void main(String args[]) {
        int a[] = {1, 2, 5, 6, 3, 2};
        int b[] = {44, 66, 99, 77, 33, 22, 55};
        System.out.println("Số nhỏ thứ 2 của Mảng a: " + getSecondSmallest(a, a.length));
    }
}


In các phần tử trùng nhau trong mảng

 In các phần tử trùng nhau trong mảng


Để in ra các phần tử trùng nhau thì chúng ta thực hiện trong 4 bước:

  • Bước 1: Duyệt từ đầu đến cuối mảng.
  • Bước 2: Triển khai vòng for j(ở trong vòng for i) bắt đầu tăng dần từ i đến hết.
  • Bước 3: Duyệt 2 vòng for, nếu arr[i] == arr[j] thì in arr[j]

public class Baitap {  
    public static void  main(String[] args) {    
            int[] arr =  new int[] { 1 ,  2 ,  3 ,  4 ,  2 ,  7 ,  8 ,  8 ,  3 };  
            System.out.println( "Chương trình này được đăng tại Freetuts.net" );  
            System.out.println( "Các phần tử trùng nhau trong mảng đã cho: " );  
            for(int i = 0; i < arr.length; i++) {  
                for(int j = i + 1; j < arr.length; j++) {  
                    if(arr[i] == arr[j])  
                        System.out.println(arr[j]);  
                }  
            }  
        }  
    }

Đảo ngược các từ trong chuỗi

 Đảo ngược các từ trong chuỗi


Ví dụ, nếu chúng ta nhập vào "HELLO" thì sau khi đảo ngược sẽ thành "OLLEH".
public class DaoChuoi
{
    public void reverseWordInMyString(String str)
    {
        String[] words = str.split(" ");
        String reversedString = "";
        for (int i = 0; i < words.length; i++)
        {
            String word = words[i];
            String reverseWord = "";
            for (int j = word.length()-1; j >= 0; j--)
            {
                reverseWord = reverseWord + word.charAt(j);
            }
            reversedString = reversedString + reverseWord + " ";
        }
        System.out.println(str);
        System.out.println(reversedString);
    }
    public static void main(String[] args)
    {
        DaoNguocChuoi obj = new DaoNguocChuoi();
        obj.reverseWordInMyString("HELLO");
    }
}

Kiểm tra chuỗi đối xứng

Kiểm tra chuỗi đối xứng

class KiemTraChuoiDoiXung {
    public static void main(String args[])
    {
        String reverseString="";
        Scanner scanner = new Scanner(System.in);
        System.out.println("Nhập vào chuỗi bạn muốn kiểm tra: ");
        String inputString = scanner.nextLine();
        int length = inputString.length();
        for ( int i = length - 1 ; i >= 0 ; i-- )
            reverseString = reverseString + inputString.charAt(i);
        if (inputString.equals(reverseString))
            System.out.println("Đây là chuỗi đối xứng!");
        else
            System.out.println("Đây không phải là chuỗi đối xứng!");
    }
}

Chuyển chữ hoa thành chữ thường

 Chuyển chữ hoa thành chữ thường



public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String message;
        System.out.println("\nNhập vào chuỗi cần in thường: ");
        message = sc.nextLine();
        System.out.println("Chuỗi sau khi in hoa: ");
        System.out.println(message.toLowerCase());
    }
}


Chuyển chữ thường thành chữ hoa

 Chuyển chữ thường thành chữ hoa



class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      String message;
      System.out.println("\n\nNhập vào chuỗi cần in hoa: ");
      message = sc.nextLine();
      System.out.println("Chuỗi sau khi in hoa: ");
      System.out.println(message.toUpperCase());
    }
  }


Liệt kê số lần xuất hiện của các phần tử

 Liệt kê số lần xuất hiện của các phần tử


Nhập số liệu cho dãy số thực a
0, a1,..., an-1. In ra màn hình số lần xuất hiện của các phần tử.


public class Main {
    public static int nhap() {
        Scanner input = new Scanner(System.in);
        boolean check = false;
        int n = 0;
        while (!check) {
            System.out.print(" ");
            try {
                n = input.nextInt();
                check = true;
            } catch (Exception e) {
                System.out.println("Ban phai nhap so! hay nhap lai...");
                input.nextLine();
            }
        }
        return (n);
    }

    public static int countElement(int a[], int n, int i) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (a[j] == i)
                count++;
        }
        return (count);
    }

    public static void main(String[] args) {
        int n, i;
        System.out.println("Nhap n= ");
        n = nhap();
        int[] array = new int[n];
        for (i = 0; i < n; i++) {
            System.out.println("Nhap phan tu thu " + (i + 1) + " ");
            array[i] = nhap();
        }
        for (i = 0; i < n; i++) {
            if (countElement(array, i, array[i]) == 0) {
                System.out.println("Phan tu " + array[i] + " xuat hien " + countElement(array, n, array[i]) + " lan");
            }
        }
    }
}