Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

โปรแกรม Java สลับองค์ประกอบแถวแรกและแถวสุดท้ายในเมทริกซ์ตามคอลัมน์


ในบทความนี้ เราจะเรียนรู้วิธีการสลับองค์ประกอบแถวแรกและแถวสุดท้ายในเมทริกซ์ตามคอลัมน์ (Interchange Elements of First and Last in a Matrix Across Columns) เมทริกซ์มีการจัดเรียงองค์ประกอบเป็นแถวและคอลัมน์ เมทริกซ์ที่มี m แถวและ n คอลัมน์ เรียกว่าเมทริกซ์ m × n องค์ประกอบแต่ละตัวในเมทริกซ์เรียกว่า element และแทนด้วย a[i][j] ซึ่งหมายถึงองค์ประกอบ a อยู่ที่แถวที่ i และคอลัมน์ที่ j

ตัวอย่างการทำงาน

สมมติข้อมูลนำเข้า (Input) คือ:

The matrix is defined as: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50

ผลลัพธ์ที่ต้องการ (Output) คือ:

The matrix after swapping the elements: 7 5 6 4 4 7 3 1 14 12 13 11 50 24 25 23

อัลกอริทึม

ขั้นตอนที่ 1 - เริ่มต้น
ขั้นตอนที่ 2 - ประกาศเมทริกซ์จำนวนเต็ม input_matrix และตัวแปรจำนวนเต็ม matrix_length
ขั้นตอนที่ 3 - กำหนดค่าให้กับเมทริกซ์
ขั้นตอนที่ 4 - วนลูปผ่านแต่ละองค์ประกอบด้วย for-loop หลายชั้น สลับองค์ประกอบที่จำเป็นโดยใช้ตัวแปรชั่วคราว (temp)
ขั้นตอนที่ 5 - แสดงผลลัพธ์
ขั้นตอนที่ 6 - จบการทำงาน

ตัวอย่างที่ 1: เขียนโค้ดทั้งหมดในฟังก์ชัน main

public class MatrixSwap { 
    static void swap(int input_matrix[][]) { 
    } 
    public static void main(String args[]) { 
        int input_matrix[][] = { 
            { 4, 5, 6, 7 }, 
            { 1, 7, 3, 4 }, 
            { 11, 12, 13, 14 }, 
            { 23, 24, 25, 50 } 
        }; 
        System.out.println("The matrix is defined as: "); 
        for (int i = 0; i < input_matrix.length; i++) { 
            for (int j = 0; j < input_matrix[0].length; j++) 
                System.out.print(input_matrix[i][j] + " "); 
            System.out.println(); 
        } 
        int matrix_length = input_matrix.length; 
        for (int i = 0; i < input_matrix[0].length; i++) { 
            int temp = input_matrix[i][0]; 
            input_matrix[i][0] = input_matrix[i][matrix_length - 1]; 
            input_matrix[i][matrix_length - 1] = temp; 
        } 
        System.out.println("\nThe matrix after swapping the elements: "); 
        for (int i = 0; i < matrix_length; i++) { 
            for (int j = 0; j < input_matrix[0].length; j++) 
                System.out.print(input_matrix[i][j] + " "); 
            System.out.println(); 
        } 
    } 
}

ผลลัพธ์

The matrix is defined as: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50
The matrix after swapping the elements: 7 5 6 4 4 7 3 1 14 12 13 11 50 24 25 23

ตัวอย่างที่ 2: ใช้ฟังก์ชันแยก (Object-Oriented Style)

public class MatrixSwap { 
    static void swap(int input_matrix[][]) { 
        int matrix_length = input_matrix.length; 
        for (int i = 0; i < input_matrix[0].length; i++) { 
            int temp = input_matrix[i][0]; 
            input_matrix[i][0] = input_matrix[i][matrix_length - 1]; 
            input_matrix[i][matrix_length - 1] = temp; 
        } 
        System.out.println("\nThe matrix after swapping the elements: "); 
        for (int i = 0; i < matrix_length; i++) { 
            for (int j = 0; j < input_matrix[0].length; j++) 
                System.out.print(input_matrix[i][j] + " "); 
            System.out.println(); 
        } 
    } 
    public static void main(String args[]) { 
        int input_matrix[][] = { 
            { 4, 5, 6, 7 }, 
            { 1, 7, 3, 4 }, 
            { 11, 12, 13, 14 }, 
            { 23, 24, 25, 50 } 
        }; 
        System.out.println("The matrix is defined as: "); 
        for (int i = 0; i < input_matrix.length; i++) { 
            for (int j = 0; j < input_matrix[0].length; j++) 
                System.out.print(input_matrix[i][j] + " "); 
            System.out.println(); 
        } 
        swap(input_matrix); 
    } 
}

ผลลัพธ์

The matrix is defined as: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50
The matrix after swapping the elements: 7 5 6 4 4 7 3 1 14 12 13 11 50 24 25 23