L1-054 福到了 (15分)(两种方法)

L1-054 福到了(15 分)

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

输入格式:

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

输出格式:

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

输入样例 1:

$ 9
 @  @@@@@
@@@  @@@ 
 @   @ @ 
@@@  @@@ 
@@@ @@@@@
@@@ @ @ @
@@@ @@@@@
 @  @ @ @
 @  @@@@@

输出样例 1:

$$$$$  $ 
$ $ $  $ 
$$$$$ $$$
$ $ $ $$$
$$$$$ $$$
 $$$  $$$
 $ $   $ 
 $$$  $$$
$$$$$  $ 

输入样例 2:

& 3
@@@
 @ 
@@@

输出样例 1:

bu yong dao le
&&&
 & 
&&&

两种方法,这是第一种,定义两个二维数组来判断是否一样。

#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
using namespace std;
int main() {
	char s; 
	int n;
	cin>>s>>n;
	getchar();
	char a[n][n];
	char b[n][n];
	bool flag=true;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%c",&a[i][j]);
		}
		getchar();//记得加getchar来消除换行
		
	}
	int p=0,q=0;
	for(int i=n-1,p=0;i>=0;i--,p++){
		for(int j=n-1,q=0;j>=0;j--,q++){
			b[p][q]=a[i][j];//b为a的反过来的
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(a[i][j]!=b[i][j]){
				flag=false;//只要有一个不一样就退出,false置为假
				break;
			}
		}
	}
	if(flag){
		cout<<"bu yong dao le"<<endl;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(b[i][j]!=' '){
					cout<<s;//不是空格,输出给的s字符
				}
				else{
					cout<<" ";
				}
			}
			cout<<endl;
		}
		
	}
	else{
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(b[i][j]!=' '){
					cout<<s;
				}
				else{
					cout<<" ";
				}
			}
			cout<<endl;
		}
	}
	
}

第二种简单一点点,在一个数组里判断即可;

#include <iostream>
#include <string>
#include<algorithm>
#include<bits/stdc++.h>
#include<stack>
#include<set>
#include <vector>
#include <map>
using namespace std;
int main() {
	char s; 
	int n;
	cin>>s>>n;
	getchar();
	char a[n][n];
	char b[n][n];
	bool flag=true;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%c",&a[i][j]);
		}
		getchar();
		
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int ii=n-1-i;
			int jj=n-1-j;
			if(a[ii][jj]!=a[i][j]){//再一个数组里判断,节省空间
				flag=false;
			}
		}
	}
	if(flag){
		cout<<"bu yong dao le"<<endl;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]!=' '){
					cout<<s;
				}
				else{
					cout<<" ";
				}
			}
			cout<<endl;
		}
		
	}
	else{
		for(int i=n-1;i>=0;i--){
			for(int j=n-1;j>=0;j--){
				if(a[i][j]!=' '){
					cout<<s;
				}
				else{
					cout<<" ";
				}
			}
			cout<<endl;
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值