题目
基础版
矩形行交换
输入5 * 5 的矩形 和两个整数n, m ( 1 <= n, m <= 5 )
将矩形的第n行和第m行交换, 并打印
样例输入:
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
1 5
样例输出:
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
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
进阶版
矩形变换
输入5 * 5 的矩形A 以及三个整数x, y, a ( x + a <= 6 , y + a <= 6 )
矩形A中的小矩形B左上角位置在第x列, 第y行. 大小为a* a
将小矩形B顺时针旋转90 °后打印
样例输入:
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
1 3 3
样例输出:
1 2 2 1 2
5 6 7 8 3
3 7 9 5 3
0 2 3 4 6
8 1 0 2 4
代码
public class Homework0619 {
public static void main ( String[ ] args) {
Scanner sc = null;
int num = 0 ;
while ( true ) {
System. out. println ( "\n************************************************************" ) ;
System. out. println ( "1、标准作业\t\t2、进阶作业\t\t3、退出" ) ;
System. out. print ( "请输入对应编号,查看对应答案: " ) ;
sc = new Scanner ( System. in) ;
num = sc. nextInt ( ) ;
if ( num == 1 ) {
System. out. println ( "\n******************标准作业******************" ) ;
basics ( ) ;
} else if ( num == 2 ) {
System. out. println ( "\n******************进阶作业******************" ) ;
advanced ( ) ;
} else if ( num == 3 ) {
System. out. println ( "\n******************系统退出******************" ) ;
break ;
}
else {
System. out. println ( "输入错误,请重新输入" ) ;
}
}
}
/ * *
* 方法-- 进阶作业
* /
public static void advanced ( ) {
String[ ] [ ] arrayA = inputArray ( ) ;
int length = arrayA[ 0 ] . length;
int [ ] para = para ( 3 , length) ;
System. out. println ( "输入的数组为" ) ;
print ( arrayA) ;
System. out. println ( "输入的参数为" ) ;
print ( para) ;
System. out. println ( "\n得到 arrayB" ) ;
String[ ] [ ] arrayB = getArrayB ( arrayA, para) ;
print ( arrayB) ;
System. out. println ( "\n******将数组 arrayB 旋转得到******" ) ;
arrayB = rotate ( arrayB) ;
print ( arrayB) ;
for ( int i = 0 ; i < para[ 2 ] ; i++ ) {
System. arraycopy ( arrayB[ i] , 0 , arrayA[ para[ 1 ] + i - 1 ] , para[ 0 ] - 1 , para[ 2 ] ) ;
}
System. out. println ( "\n******新数组 arrayA 旋转得到******" ) ;
print ( arrayA) ;
}
public static String[ ] [ ] getArrayB ( String[ ] [ ] arrayA, int [ ] para) {
String[ ] [ ] arrayB = new String [ para[ 2 ] ] [ para[ 2 ] ] ;
for ( int i = 0 ; i < para[ 2 ] ; i++ ) {
System. arraycopy ( arrayA[ para[ 1 ] + i - 1 ] , para[ 0 ] - 1 , arrayB[ i] , 0 , para[ 2 ] ) ;
}
return arrayB;
}
public static String[ ] [ ] rotate ( String[ ] [ ] arrayB) {
int count = arrayB. length;
String[ ] [ ] change = new String [ count] [ count] ;
String[ ] temp = new String [ count] ;
for ( int i = 0 ; i < count; i++ ) {
for ( int j = 0 ; j < count; j++ ) {
temp[ j] = arrayB[ count - 1 - j] [ i] ;
}
for ( int j = 0 ; j < temp. length; j++ ) {
change[ i] [ j] = temp[ j] ;
}
}
return change;
}
/ * *
* 方法-- 标准作业
* /
public static void basics ( ) {
String[ ] [ ] array = inputArray ( ) ;
int length = array[ 0 ] . length;
int [ ] para = para ( 2 , length) ;
System. out. println ( "输入的数组为" ) ;
print ( array) ;
System. out. println ( "输入的参数为" ) ;
print ( para) ;
System. out. println ( "\n******结果******" ) ;
String[ ] temp = new String [ length] ;
System. arraycopy ( array[ para[ 0 ] - 1 ] , 0 , temp, 0 , length) ;
System. arraycopy ( array[ para[ 1 ] - 1 ] , 0 , array[ para[ 0 ] - 1 ] , 0 , length) ;
System. arraycopy ( temp, 0 , array[ para[ 1 ] - 1 ] , 0 , length) ;
print ( array) ;
}
/ * *
* 打印数组
* @param str
* /
public static void print ( int [ ] str) {
for ( int i = 0 ; i < str. length; i++ ) {
System. out. print ( str[ i] + "\t" ) ;
}
System. out. println ( ) ;
}
public static void print ( String[ ] [ ] str) {
for ( int i = 0 ; i < str. length; i++ ) {
for ( int j = 0 ; j < str[ i] . length; j++ ) {
System. out. print ( str[ i] [ j] + "\t" ) ;
}
System. out. println ( ) ;
}
}
public static boolean judge ( ) {
System. out. print ( "\n是否继续?输入y继续计算,其他则退出: " ) ;
Scanner sc = new Scanner ( System. in) ;
if ( "y" . equalsIgnoreCase ( sc. next ( ) ) ) {
System. out. print ( "继续判断," ) ;
return true ;
} else {
System. out. println ( "退出计算!" ) ;
return false ;
}
}
public static String[ ] [ ] inputArray ( ) {
Scanner sc = new Scanner ( System. in) ;
System. out. println ( "\n***输入 n*n 的数组***\n" ) ;
System. out. print ( "请输入第1行(用空格隔开):" ) ;
String str1 = sc. nextLine ( ) ;
String[ ] input1 = str1. trim ( ) . split ( "\\s+" ) ;
int count = input1. length;
String str_temp = null;
String[ ] input_temp = new String [ count] ;
String[ ] [ ] input = new String [ count] [ count] ;
input[ 0 ] = input1;
for ( int i = 1 ; i < count; i++ ) {
while ( true ) {
System. out. print ( "请输入第" + ( i + 1 ) + "行(用空格隔开):" ) ;
str_temp = sc. nextLine ( ) ;
input_temp = str_temp. trim ( ) . split ( "\\s+" ) ;
if ( input_temp. length == count) {
input[ i] = input_temp;
break ;
} else {
System. out. println ( "**请输入" + count + "个字符**" ) ;
}
}
}
return input;
}
public static int [ ] para ( int num, int length) {
System. out. println ( "\n***请输入参数***\n" ) ;
int [ ] para = new int [ num] ;
int temp = 0 ;
if ( num == 2 ) {
System. out. println ( "请依次输入2个参数,表示需要交换的行" ) ;
} else {
System. out. println ( "请依次输入3个参数,分别表示小矩形B左上角位置在矩形A中的第x列, 第y行. 大小为a*a" ) ;
}
for ( int i = 0 ; i < 2 ; i++ ) {
System. out. print ( "请输入第 " + ( i + 1 ) + " 个参数:" ) ;
para[ i] = input ( length) ;
}
if ( num == 3 ) {
System. out. print ( "请输入第 3 个参数:" ) ;
int count = 0 ;
while ( true ) {
count = input ( length) ;
if ( para[ 0 ] + count <= 6 && para[ 1 ] + count <= 6 ) {
para[ 2 ] = count;
break ;
} else {
System. out. println ( "第3个参数分别与前两个参数的和应不大于 " + ( length + 1 ) ) ;
continue ;
}
}
}
System. out. println ( ) ;
return para;
}
public static int input ( int length) {
Scanner sc = null;
int num = 0 ;
boolean flag = false ;
while ( true ) {
sc = new Scanner ( System. in) ;
flag = sc. hasNextInt ( ) ;
if ( flag) {
num = sc. nextInt ( ) ;
if ( num > 0 && num <= length) {
return num;
} else {
System. out. print ( "输入错误,请输入一个 1~" + length + " 的数: " ) ;
}
} else {
System. out. print ( "输入错误,请输入一个整数: " ) ;
}
}
}
}
运行结果
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 、标准作业 2 、进阶作业 3 、退出
请输入对应编号,查看对应答案: 1
* * * * * * * * * * * * * * * * * * 标准作业* * * * * * * * * * * * * * * * * *
* * * 输入 n* n 的数组* * *
请输入第1 行( 用空格隔开) :11 12 13 14 15
请输入第2 行( 用空格隔开) :21 22 23 24 25
请输入第3 行( 用空格隔开) :31 32 33 34 35
请输入第4 行( 用空格隔开) :41 42 43 44 45
请输入第5 行( 用空格隔开) :51 52 53 54 55
* * * 请输入参数* * *
请依次输入2 个参数,表示需要交换的行
请输入第 1 个参数:1
请输入第 2 个参数:5
输入的数组为
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
输入的参数为
1 5
* * * * * * 结果* * * * * *
51 52 53 54 55
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
11 12 13 14 15
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 、标准作业 2 、进阶作业 3 、退出
请输入对应编号,查看对应答案: 2
* * * * * * * * * * * * * * * * * * 进阶作业* * * * * * * * * * * * * * * * * *
* * * 输入 n* n 的数组* * *
请输入第1 行( 用空格隔开) :11 12 13 14 15
请输入第2 行( 用空格隔开) :21 22 23 24 25
请输入第3 行( 用空格隔开) :31 32 33 34 35
请输入第4 行( 用空格隔开) :41 42 43 44 45
请输入第5 行( 用空格隔开) :51 52 53 54 55
* * * 请输入参数* * *
请依次输入3 个参数,分别表示小矩形B左上角位置在矩形A中的第x列, 第y行. 大小为a* a
请输入第 1 个参数:1
请输入第 2 个参数:3
请输入第 3 个参数:3
输入的数组为
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
输入的参数为
1 3 3
得到 arrayB
31 32 33
41 42 43
51 52 53
* * * * * * 将数组 arrayB 旋转得到* * * * * *
51 41 31
52 42 32
53 43 33
* * * * * * 新数组 arrayA 旋转得到* * * * * *
11 12 13 14 15
21 22 23 24 25
51 41 31 34 35
52 42 32 44 45
53 43 33 54 55
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1 、标准作业 2 、进阶作业 3 、退出
请输入对应编号,查看对应答案: 3
* * * * * * * * * * * * * * * * * * 系统退出* * * * * * * * * * * * * * * * * *