L1-054 福到了
“福”字倒着贴,寓意“福到”。不论到底算不算民俗,本题且请你编写程序,把各种汉字倒过来输出。这里要处理的每个汉字是由一个 N × N 的网格组成的,网格中的元素或者为字符 @
或者为空格。而倒过来的汉字所用的字符由裁判指定。
输入格式:
输入在第一行中给出倒过来的汉字所用的字符、以及网格的规模 N (不超过 100 的正整数),其间以 1 个空格分隔;随后 N 行,每行给出 N 个字符,或者为 @
或者为空格。
输出格式:
输出倒置的网格,如样例所示。但是,如果这个字正过来倒过去是一样的,就先输出bu yong dao le
,然后再用输入指定的字符将其输出。
输入样例 1:
tex
$ 9
@ @@@@@
@@@ @@@
@ @ @
@@@ @@@
@@@ @@@@@
@@@ @ @ @
@@@ @@@@@
@ @ @ @
@ @@@@@
$ 9
@ @@@@@
@@@ @@@
@ @ @
@@@ @@@
@@@ @@@@@
@@@ @ @ @
@@@ @@@@@
@ @ @ @
@ @@@@@
输出样例 1:
tex
$$$$$ $
$ $ $ $
$$$$$ $$$
$ $ $ $$$
$$$$$ $$$
$$$ $$$
$ $ $
$$$ $$$
$$$$$ $
$$$$$ $
$ $ $ $
$$$$$ $$$
$ $ $ $$$
$$$$$ $$$
$$$ $$$
$ $ $
$$$ $$$
$$$$$ $
输入样例 2:
tex
& 3
@@@
@
@@@
& 3
@@@
@
@@@
输出样例 2:
tex
bu yong dao le
&&&
&
&&&
bu yong dao le
&&&
&
&&&
Solution:
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = in.readLine().split(" ");
String s = input[0];
int n = Integer.parseInt(input[1]);
char[][] shape = new char[n][n];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
String t = in.readLine();
for (int j = 0; j < n; j++) {
char c = t.charAt(j);
shape[i][j] = c;
sb.append(c);
}
if (i != n - 1) sb.append("\n");
}
StringBuilder fall = new StringBuilder();
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
fall.append(shape[i][j]);
}
if (i != 0) fall.append("\n");
}
if (sb.toString().equals(fall.toString())) {
System.out.println("bu yong dao le");
}
System.out.println(fall.toString().replace("@", s));
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] input = in.readLine().split(" ");
String s = input[0];
int n = Integer.parseInt(input[1]);
char[][] shape = new char[n][n];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
String t = in.readLine();
for (int j = 0; j < n; j++) {
char c = t.charAt(j);
shape[i][j] = c;
sb.append(c);
}
if (i != n - 1) sb.append("\n");
}
StringBuilder fall = new StringBuilder();
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
fall.append(shape[i][j]);
}
if (i != 0) fall.append("\n");
}
if (sb.toString().equals(fall.toString())) {
System.out.println("bu yong dao le");
}
System.out.println(fall.toString().replace("@", s));
}
}