[英]How to take input while printing mirror image if mirror is placed along one of the sides of the array
Given a two dimensional array, print its mirror image if mirror is placed along one of the sides of the array.
给定一个二维数组,如果将镜像放置在数组的一侧,则打印其镜像。
First line of input will contain a number T = number of test cases. 输入的第一行将包含数字T =测试用例数。 Each test case will contain two positive integers n and m (1<=n, m<=50) on a single line separated by space.
每个测试用例在由空格分隔的一行上将包含两个正整数n和m(1 <= n,m <= 50)。 Next n lines will each contain a string of exactly m characters.
接下来的n行将每行包含正好是m个字符的字符串。 Next line will contain a character 'V' or 'H'.
下一行将包含字符“ V”或“ H”。 If character is V, mirror is placed vertically along the right-most column.
如果字符是V,则将镜面沿着最右列垂直放置。 If the character is H, the mirror is placed horizontally along the bottom-most row.
如果字符为H,则将镜子沿最底行水平放置。
For each test case, print the n*m mirror image - n lines with strings of m character each. 对于每个测试用例,打印n * m镜像-n行,每行m个字符。 Print an extra empty line after output for each test case.
在为每个测试用例输出后,再打印一个空行。
2
3 3
abc
def
ghi
V
3 4
1234
5678
9876
H
cba
fed
ihg
9876
5678
1234
MyApproach: 我的方法:
When I wrote the following Code.I am having problem while taking input. 当我编写以下代码时,输入时遇到问题。 How to stop taking the input when the length of the input become equal to m characters.
当输入的长度等于m个字符时如何停止输入。
Below is the code 下面是代码
int arr[][]=new int[n][m];
for(int j=0;j<n;j++)
{
for(int k=0;k<m;k++)
{
arr[j][k]=sc.nextInt();
//but if the input is in character how can i stop
//I think I need to read the characters character by character and stop hen m==3(as per Sample Input)
//How can I do that in java
}
System.out.println();
}
Try this 尝试这个
static void swap(String[][] array, int r1, int c1, int r2, int c2) {
String temp = array[r1][c1];
array[r1][c1] = array[r2][c2];
array[r2][c2] = temp;
}
static void vertical(String[][] array) {
int rows = array.length;
int cols = array[0].length;
for (int r = 0; r < rows; ++r)
for (int c = 0; c < cols / 2; ++c)
swap(array, r, c, r, cols - c - 1);
}
static void horizontal(String[][] array) {
int rows = array.length;
int cols = array[0].length;
for (int c = 0; c < cols; ++c)
for (int r = 0; r < rows / 2; ++r)
swap(array, r, c, rows - r - 1, c);
}
public static void main(String[] args) {
String s = ""
+ "2\n"
+ "3 3\n"
+ "abc\n"
+ "def\n"
+ "ghi\n"
+ "V\n"
+ "3 4\n"
+ "1234\n"
+ "5678\n"
+ "9876\n"
+ "H\n";
try (Scanner scanner = new Scanner(s)) {
int cases = scanner.nextInt();
for (int i = 0; i < cases; ++i) {
int rows = scanner.nextInt();
int cols = scanner.nextInt();
scanner.nextLine();
String[][] array = new String[rows][cols];
for (int r = 0; r < rows; ++r)
array[r] = scanner.nextLine().split("");
String operation = scanner.nextLine();
if (operation.equals("H"))
horizontal(array);
else
vertical(array);
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c)
System.out.print(array[r][c]);
System.out.println();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.