JavaStudy——0078:矩陣交換行
阿新 • • 發佈:2018-11-15
總時間限制: 1000ms 記憶體限制: 65536kB
描述
編寫一個函式,輸入引數是55的二維陣列,和n,m兩個行下標。功能:判斷n,m是否在陣列範圍內,如果不在,則返回0;如果在範圍內,則將n行和m行交換,並返回1。
在main函式中, 生成一個55的矩陣,輸入矩陣資料,並輸入n,m的值。呼叫前面的函式。如果返回值為0,輸出error。如果返回值為1,輸出交換n,m後的新矩陣。
輸入
5*5矩陣的資料,以及n和m的值。
輸出
如果不可交換,則輸出error;
如果可交換,則輸出新矩陣
樣例輸入
1 2 2 1 2 5 6 7 8 3 9 3 0 5 3 7 2 1 4 6 3 0 8 2 4 0 4
樣例輸出
3 0 8 2 4
5 6 7 8 3
9 3 0 5 3
7 2 1 4 6
1 2 2 1 2
Accepted程式碼
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int[][] a=new int[5][5];
for(int i=0;i<5;i++) {
for(int j=0;j< 5;j++) {
a[i][j]=in.nextInt();
}
}
int m=in.nextInt();
int n=in.nextInt();
if(IsInScope(m,n)) {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
int k=a[m][j];
a[m][j]=a[n] [j];
a[n][j]=k;
System.out.printf("%4d",a[i][j]);
}
System.out.println();
}
}
else
System.out.print("error");
in.close();
}
public static boolean IsInScope(int x,int y) {
if((x>=0&&x<5)&&(y>=0&&y<5)) return true;
return false;
}
}