题目背景
知名魔术师 Al'Dimi Kartimi 需要你帮助他来设计他扑克牌的背面图案。
输入输出样例
输入 #1
2 2
#.
.#
3 3
输出 #1
#..#
.##.
.#..
#..#
输入 #2
3 3
###
###
###
1 4
输出 #2
###.##
######
######
######
######
######
输入 #3
5 4
#.#.
#.##
#.##
....
.#.#
10 5
输出 #3
#.#..#.#
#.####.#
#.####.#
........
.#.##.#.
.#.##.#.
........
#.####.#
#.####.#
#.#.##.#
代码1:
字符数组,一个一个赋值
#include<iostream>
#include<string>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
char s[150][150];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> s[i][j];
}
}
int p = n;
for (int i = 1; i <= m; i++) {
p = n;
for (int j = n + 1; j <= 2 * n; j++) {
s[i][j] = s[i][p];
p--;
}
}
p = m;
for (int i = m + 1; i <= 2 * m; i++) {
for (int j = 1; j <= 2 * n; j++) {
s[i][j] = s[p][j];
}
p--;
}
int x, y;
cin >> x >> y;
if (s[x][y] == '.') {
s[x][y] = '#';
}
else {
s[x][y] = '.';
}
for (int i = 1; i <= 2 * m; i++) {
for (int j = 1; j <= 2 * n; j++) {
cout << s[i][j];
}
cout << endl;
}
return 0;
}
代码2:
使用reverse函数会使代码简化很多
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int m, n;
cin >> m >> n;
string s[150];
for (int i = 1; i <= m; i++) {
cin >> s[i];
string temp = s[i];
reverse(temp.begin(), temp.end());
s[i] += temp;
}
int p = 1;
for (int i = 2 * m; i >= m; i--) {
s[i] = s[p++];
}
int x, y;
cin >> x >> y;
if (s[x][y-1] == '.') {
s[x][y-1] = '#';
}
else {
s[x][y-1] = '.';
}
for (int i = 1; i <= 2 * m; i++) {
cout << s[i] << endl;
}
return 0;
}