Java程序创建矩阵并用回文数填充
在Java中,可以使用二维数组表示矩阵。矩阵用于存储和处理具有表格结构的数据。矩阵具有与其相关的多个属性和操作,例如加法、减法、乘法、转置和行列式计算。
根据问题陈述,我们必须创建一个矩阵并用回文数填充它。
让我们开始吧!
例如
假设我们有一个4*5的矩阵
对矩阵执行操作后,结果将为
输入行数:4
输入列数:5
包含回文数的矩阵
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111
算法
步骤1:获取矩阵的大小。
步骤2:创建一个方阵。
步骤3:检查回文数并将其插入矩阵。
步骤4:打印结果。
多种方法
我们提供了不同方法的解决方案。
使用矩阵元素的静态初始化
使用用户自定义方法
让我们逐一查看程序及其输出。
方法1:使用数组元素的静态初始化
在这种方法中,矩阵元素将在程序中初始化。然后根据算法创建矩阵并用回文数填充它。
示例
import java.util.Scanner;
public class Main
{
// main method
public static void main(String[] args)
{
// taking the size of the matrix
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
// create a square matrix
int[][] matrix = new int[rows][columns];
// Fill the matrix with palindrome numbers
fillMatrixWithPalindromes(matrix);
// Print the matrix
System.out.println("Matrix with Palindrome Numbers:");
printMatrix(matrix);
}
// Function to create a matrix and fill it with Palindrome numbers
public static void fillMatrixWithPalindromes(int[][] matrix) {
int num = 1;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
// Find the next palindrome number
while (!isPalindrome(num)) {
num++;
}
// Fill the matrix with the palindrome number
matrix[i][j] = num;
num++;
}
}
}
// Function to check if a number is an Palindrome number or not
public static boolean isPalindrome(int number) {
String str = String.valueOf(number);
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
// Function to display the matrix
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
输出
Enter the number of rows: 4 Enter the number of columns: 5 Matrix with Palindrome Numbers: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111
方法2:使用用户自定义方法
在这种方法中,矩阵元素将在程序中初始化。然后通过将矩阵作为参数传递给用户自定义方法,并在方法内部根据算法创建矩阵并用回文数填充它。
示例
import java.util.Scanner;
public class Main
{
// main method
public static void main(String[] args)
{
// taking the size of the matrix
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
// calling user defined method
func(rows, columns);
}
// user defined method
public static void func(int rows, int columns)
{
// create a square matrix
int[][] matrix = new int[rows][columns];
// Fill the matrix with palindrome numbers
fillMatrixWithPalindromes(matrix);
// Print the matrix
System.out.println("Matrix with Palindrome Numbers:");
printMatrix(matrix);
}
// Function to create a matrix and fill it with Palindrome numbers
public static void fillMatrixWithPalindromes(int[][] matrix) {
int num = 1;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
// Find the next palindrome number
while (!isPalindrome(num)) {
num++;
}
// Fill the matrix with the palindrome number
matrix[i][j] = num;
num++;
}
}
}
// Function to check if a number is an Palindrome number or not
public static boolean isPalindrome(int number) {
String str = String.valueOf(number);
int i = 0;
int j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
// Function to display the matrix
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
输出
Enter the number of rows: 5 Enter the number of columns: 5 Matrix with Palindrome Numbers: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161
在本文中,我们探讨了如何使用Java编程语言创建矩阵并用回文数填充它。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP