Matrix 如何按升序排列矩阵的边界元素?
我已经做过了,但是当我进入矩阵时,矩阵中的所有元素都被排序了!但我只想按升序对边界元素进行排序。有人能告诉我我的错误吗Matrix 如何按升序排列矩阵的边界元素?,matrix,Matrix,我已经做过了,但是当我进入矩阵时,矩阵中的所有元素都被排序了!但我只想按升序对边界元素进行排序。有人能告诉我我的错误吗 int k,temp=0,sum=0; k=n; boolean b=true; do { for(i=0;i<m;i++) { for(j=0;j<k-1;j++) { if
int k,temp=0,sum=0;
k=n;
boolean b=true;
do
{
for(i=0;i<m;i++)
{
for(j=0;j<k-1;j++)
{
if(i!=0||j!=0)
{
if(A[i][j]>A[i][j+1])
{
temp=A[i][j];
A[i][j]=A[i][j+1];
A[i][j+1]=temp;
}
}
}
}
k-=1;
if(k<0)
b=false;
}while(b);
k=m;
do
{
for(i=0;i<k-1;i++)
{
for(j=0;j<n;j++)
{
if(i!=0||j!=0)
{
if(A[j][i]>A[j][i+1])
{
temp=A[j][i];
A[j][i]=A[j][i+1];
A[j][i+1]=temp;
}
}
}
}
k-=1;
if(k<0)
b=false;
}while(b);
System.out.println("REARRANGED MATRIX:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+" ");
}
System.out.println();
}
intk,温度=0,总和=0;
k=n;
布尔b=真;
做
{
对于(i=0;i而不是使用条件“if(i!=0 | | | j!=0)”使用“if(i==0 | | i==2 | | j==0 | | j==2)”。这可能会解决您的歧义。您的错误是,您将行数和列数都设置为大于零。边界元素是那些行数为0或2或列数为0或2的元素(我的意思是,只有那些坐标为I=0或I=2或j=0或j=2的元素才会被视为边界元素。我有一个解决方案。我使用了选择排序。首先我对矩阵进行排序,然后显示排序数组的边界
import java.io.*;
class Boundary_Sorting
{
public static void main(String args[])throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter the rows of the matrix=");
int m = Integer.parseInt(br.readLine());
System.out.println("Enter the column of the matrix=");
int n = Integer.parseInt(br.readLine());
int a[][] = new int[m][n];
int i,j;
System.out.println("Enter the elements of the matrix: ");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("**********************");
System.out.println("The original matrix is");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
int B[] = new int[m*n]; //creating a 1D Array of size 'r*c'
int x = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
B[x] = a[i][j];
x++;
}
}
/*Sorting the 1D Array in Ascending Order*/
int t = 0;
for(i = 0; i < (m * n) - 1; i++)
{
for(j = i + 1; j < (m * n); j++)
{
if(B[i] > B[j])
{
t = B[i];
B[i] = B[j];
B[j] = t;
}
}
}
/*Saving the sorted 1D Array back into the 2D Array */
x = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
a[i][j] = B[x];
x++;
}
}
/* Printing the sorted 2D Array */
System.out.println("**********************");
System.out.println("The Sorted Array:");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("**********************");
System.out.println("The boundary elements of the matrix is=");
System.out.println("**********************");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(i==0 || j==0 || i == m-1 || j == n-1) //condition for accessing boundary elements
System.out.print(a[i][j]+"\t");
else
System.out.print(" \t");
}
System.out.println();
}
}
}
import java.io.*;
类边界排序
{
公共静态void main(字符串args[])引发IOException
{
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader br=新的BufferedReader(isr);
System.out.println(“输入矩阵的行=”);
int m=Integer.parseInt(br.readLine());
System.out.println(“输入矩阵的列=”);
int n=Integer.parseInt(br.readLine());
整数a[][]=新整数[m][n];
int i,j;
System.out.println(“输入矩阵的元素:”);
对于(i=0;iB[j])
{
t=B[i];
B[i]=B[j];
B[j]=t;
}
}
}
/*将排序后的一维数组保存回二维数组*/
x=0;
对于(i=0;i