L1-054. 福到了

“福”字倒着贴,寓意“福到”。不论到底算不算民俗,本题且请你编写程序,把各种汉字倒过来输出。这里要处理的每个汉字是由一个 N x N 的网格组成的,网格中的元素或者为字符“@”或者为空格。而倒过来的汉字所用的字符由裁判指定。

输入格式:

输入在第一行中给出倒过来的汉字所用的字符、以及网格的规模 N (不超过100的正整数),其间以 1 个空格分隔;随后 N 行,每行给出 N 个字符,或者为“@”或者为空格。

输出格式:

输出倒置的网格,如样例所示。但是,如果这个字正过来倒过去是一样的,就先输出“bu yong dao le”,然后再用输入指定的字符将其输出。

输入样例 1:
$ 9
 @  @@@@@
@@@  @@@ 
 @   @ @ 
@@@  @@@ 
@@@ @@@@@
@@@ @ @ @
@@@ @@@@@
 @  @ @ @
 @  @@@@@
输出样例 1:
$$$$$  $ 
$ $ $  $ 
$$$$$ $$$
$ $ $ $$$
$$$$$ $$$
 $$$  $$$
 $ $   $ 
 $$$  $$$
$$$$$  $ 
输入样例 2:
& 3
@@@
 @ 
@@@
输出样例 2:
bu yong dao le
&&&
 & 
&&&

对角判断方阵的字符是否相等。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
char s[101][101],ch[2];
int d;
int check()
{
    for(int i = 0;i < d;i ++)
    {
        for(int j = 0;j < d;j ++)
        {
            if(s[i][j] != s[d - i - 1][d - j - 1])return 0;
        }
    }
    return 1;
}
int main()
{
    cin>>ch>>d;
    cin.get();
    for(int i = 0;i < d;i ++)
    {
        gets(s[i]);
    }
    if(check())
    {
        cout<<"bu yong dao le"<<endl;
    }
    for(int i = d - 1;i >= 0;i --)
    {
        for(int j = d - 1;j >= 0;j --)
        if(s[i][j] != ' ')cout<<ch[0];
        else cout<<s[i][j];
        cout<<endl;
    }
}

 重温:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
char s[101][101];
char ch[2];
int n;
bool check() {
    for(int i = 0;i < n;i ++) {
        for(int j = 0;j < n / 2;j ++) {
            if(s[i][j] != s[n - 1 - i][n - 1 - j]) return false;
        }
    }
    return true;
}
int main() {
    scanf("%s%d",ch,&n);
    for(int i = 0;i < n;i ++) {
        getchar();
        for(int j = 0;j < n;j ++) {
            s[i][j] = getchar();
            if(s[i][j] == '@') s[i][j] = ch[0];
        }
        s[i][n] = 0;
    }
    if(check()) {
        puts("bu yong dao le");
    }
    for(int i = n - 1;i >= 0;i --) {
        for(int j = n - 1;j >= 0;j --) {
            putchar(s[i][j]);
        }
        putchar('
');
    }
}
原文地址:https://www.cnblogs.com/8023spz/p/8719117.html