总时间限制: 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;
}
}