当前位置:网站首页>7-5 blessing arrived
7-5 blessing arrived
2022-07-06 16:43:00 【HBUcs2020】
“ blessing ” The words are pasted upside down , Moral “ Blessed ”. Whether it's folklore or not , Please write the program , Output all kinds of Chinese characters upside down . Each character to be processed here is made up of a N × N It's a grid of , The elements in the grid are either characters @
Or space . The characters used in the inverted Chinese characters are designated by the referee .
Input format :
Enter the character used to give the inverted Chinese character in the first line 、 And the size of the grid N ( No more than 100 The positive integer ), In the meantime 1 Space separation ; And then N That's ok , Each line gives N Characters , Or for @
Or space .
Output format :
Output inverted grid , As the example shows . however , If the word is the same in the reverse , First output bu yong dao le
, And then input the specified character to output it .
sample input 1:
$ 9
@ @@@@@
@@@ @@@
@ @ @
@@@ @@@
@@@ @@@@@
@@@ @ @ @
@@@ @@@@@
@ @ @ @
@ @@@@@
sample output 1:
$$$$$ $
$ $ $ $
$$$$$ $$$
$ $ $ $$$
$$$$$ $$$
$$$ $$$
$ $ $
$$$ $$$
$$$$$ $
sample input 2:
& 3
@@@
@
@@@
sample output 2:
bu yong dao le
&&&
&
&&&
Ideas
To start with reverse Function to reverse a two-dimensional array , But the pointer problem of two-dimensional array is found in the operation , Can only reverse every line , There is no way to reverse the column
And then use string Instead of , The storage structure type is vector<string> s
#include<bits/stdc++.h>
using namespace std;
vector<string> s;
int main(){
char c;
int n;
cin>>c>>n;
getchar();
for(int i=0;i<n;i++)
{
string ss;
getline(cin,ss);
s.push_back(ss);
}
int f=0;
for(int i=0;i<n;i++)
{
if(s[i]!=s[n-i-1])
{
f=1;
break;
}
}
vector<string> ans;
for(int i=s.size()-1;i>=0;i--)
{
string sss;
for(int j=s[i].size()-1;j>=0;j--)
{
if(s[i][j]!=' ') sss+=c;
else sss+=' ';
}
ans.push_back(sss);
}
if(f==1)
for(auto x:ans) cout<<x<<endl;
else{
cout<<"bu yong dao le"<<endl;
for(auto x:ans) cout<<x<<endl;
}
return 0;
}
Solution 2 :
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
string s,a;
int i,n,ret=0;
char b;
cin>>b>>n;
cin.get();
for(i=0;i<n;++i)
{
getline(cin,a);
s+=a;
}
string s1=s;
reverse(s1.begin(),s1.end());
if(s1==s)
cout<<"bu yong dao le"<<endl;
for(i=0;i<s1.length();++i)
{
ret++;
if(s1[i]!=' ')
s1[i]=b;
cout<<s1[i];
if(ret==n)
{
cout<<endl;
ret=0;
}
}
}
solution 3:
There is also the problem of symmetry ,
#include<iostream>
using namespace std;
#include<vector>
#include<cstring>
#include<algorithm> //reverse The header file
int main()
{
vector<string>v(100);
int n;
char c;
cin>>c>>n;
getchar();
for(int i=0; i<n; i++)
{
string s;
getline(cin,s); // The header file string
reverse(s.begin(),s.end());
v.push_back(s);
}
// Judge symmetry
int flag=0;
for(int i=0; i<n; i++)
{
// for(int j=0; j<n; j++)
// {
// cout<<v[i]<<" ";
// if(v[i][j]!=v[i][n-j-1])
// {
// flag=1;
// break;
// }
// }
if(v[i]!=v[n-i-1])
{
flag=1;
}
// printf("\n");
}
reverse(v.begin(),v.end());
if(flag==0)
cout<<"bu yong dao le"<<endl;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(v[i][j]==' ')
cout<<" ";
else
cout<<c;
}
cout<<endl;
}
return 0;
}
It's solved
Use vector<int> v; Declare a container v when , If you don't reserve storage space for him ( Such as :vector<int> v;),
Can be used directly v.push_back(x) Insert the variable x, Then the first element inserted can be used v[0] Access to the .
Use vector<int> v(n); Declare a container v when , If you reserve storage space for him ( Such as :vector<int> v(n);)be vector<int> v(n) Equivalent to vector<int> v(n,0);
If you want to make the position 0 Storage elements x, You can only use v[0]=x, If you use v.insert(x) Insert the variable x, that v The first element of is 0, namely v[0]=0, because v.push_back(x) Yes, it will x Insert into v[n], Also because of the statement v when ,v It can store at most n Elements , namely x Failed to insert container v in .
#include<iostream>
using namespace std;
#include<vector>
#include<cstring>
#include<algorithm> //reverse The header file
int main()
{
int n;
char c;
cin>>c>>n;
vector<string>v(n);
getchar();
for(int i=0; i<n; i++)
{
string s;
getline(cin,s); // The header file string
reverse(s.begin(),s.end());
v.push_back(s);
}
reverse(v.begin(),v.end());
// Judge symmetry
int flag=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
//cout<<v[i][j];
if(v[i][j]!=v[n-i-1][n-j-1])
{
flag=1;
break;
}
}
}
if(flag==0)
cout<<"bu yong dao le"<<endl;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(v[i][j]==' ')
cout<<" ";
else
cout<<c;
}
cout<<endl;
}
return 0;
}
The reason lies in vector The space should be n, Error due to reverse function iterator access ----vector<string>v(n);
I think the simplest solution
//vector Container of reverse usage
#include<iostream>
using namespace std;
#include<vector>
#include<cstring>
#include<algorithm> //reverse The header file
int main()
{
int n;
char c;
cin>>c>>n;
vector<string>v(n); // Order of attention
getchar();
for(int i=0; i<n; i++)
{
string s;
getline(cin,s); // The header file string
reverse(s.begin(),s.end());
v.push_back(s);
// Method 2
// getline(cin,v[i]);
// reverse(v[i].begin(),v[i].end());
}
// Judge symmetry
//printf("\n");
/*
int flag=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
//cout<<v[i][j];
if(v[i][j]!=v[i][n-j-1])
{
flag=1;
//break;
}
}
if(v[i]!=v[n-i-1])
{
flag=1;
// break;
}
//printf("\n");
}
*/
// int f=0;
// for(int i=0; i<n; i++)
// {
// for(int j=0; j<n; j++)
// if(v[i][j]!=v[n-i-1][n-j-1])
// {
// f=1;
// break;
// }
// }
reverse(v.begin(),v.end()); // Note the iterator scope
int f=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
if(v[i][j]!=v[n-i-1][n-j-1])
{
f=1;
break;
}
}
if(f==0)
cout<<"bu yong dao le"<<endl;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(v[i][j]==' ')
cout<<" ";
else
cout<<c;
}
cout<<endl;
}
return 0;
}
边栏推荐
- 业务系统从Oracle迁移到openGauss数据库的简单记录
- ffmpeg命令行使用
- 提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
- Double specific tyrosine phosphorylation regulated kinase 1A Industry Research Report - market status analysis and development prospect prediction
- Kubernetes集群部署
- 第2章 HFDS的Shell操作
- Advancedinstaller installation package custom action open file
- Chapter 2 shell operation of hfds
- Acwing - game 55 of the week
- 875. Leetcode, a banana lover
猜你喜欢
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Chapter 7__ consumer_ offsets topic
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
Solve the single thread scheduling problem of intel12 generation core CPU (II)
SQL快速入门
Detailed explanation of FLV format
Install Jupiter notebook under Anaconda
顺丰科技智慧物流校园技术挑战赛(无t4)
图像处理一百题(11-20)
视频压缩编码和音频压缩编码基本原理
随机推荐
MariaDB的安装与配置
Codeforces Round #800 (Div. 2)AC
Generate random password / verification code
LeetCode 1557. The minimum number of points that can reach all points
Li Kou - 298th weekly match
How to insert mathematical formulas in CSDN blog
Two weeks' experience of intermediate software designer in the crash soft exam
Codeforces Global Round 19
解决Intel12代酷睿CPU单线程调度问题(二)
Educational Codeforces Round 122 (Rated for Div. 2)
Gridhome, a static site generator that novices must know
Chapter III principles of MapReduce framework
Codeforces Round #803 (Div. 2)A~C
Kubernetes集群部署
One hundred questions of image processing (1-10)
Discussion on QWidget code setting style sheet
第6章 Rebalance详解
解决Intel12代酷睿CPU单线程只给小核运行的问题
js封装数组反转的方法--冯浩的博客
Oneforall installation and use