当前位置:网站首页>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;
}
边栏推荐
- QT realizes window topping, topping state switching, and multi window topping priority relationship
- Research Report on market supply and demand and strategy of China's four seasons tent industry
- Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
- Chapter 2 shell operation of hfds
- Summary of game theory
- (lightoj - 1369) answering queries (thinking)
- LeetCode 1560. The sector with the most passes on the circular track
- Simple records of business system migration from Oracle to opengauss database
- 第2章 HFDS的Shell操作
- China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
猜你喜欢
One hundred questions of image processing (11-20)
Two weeks' experience of intermediate software designer in the crash soft exam
Li Kou - 298th weekly match
Log statistics (double pointer)
Chapter 5 namenode and secondarynamenode
300th weekly match - leetcode
QT implementation window gradually disappears qpropertyanimation+ progress bar
QT realizes window topping, topping state switching, and multi window topping priority relationship
SQL快速入门
第7章 __consumer_offsets topic
随机推荐
Discussion on QWidget code setting style sheet
Educational Codeforces Round 122 (Rated for Div. 2)
力扣leetcode第 280 场周赛
Two weeks' experience of intermediate software designer in the crash soft exam
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
SQL quick start
Calculate the time difference
Simply try the new amp model of deepfacelab (deepfake)
Cmake error: could not create named generator visual studio 16 2019 solution
< li> dot style list style type
Installation and configuration of MariaDB
Codeforces round 797 (Div. 3) no f
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
CMake速成
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
第6章 DataNode
Summary of FTP function implemented by qnetworkaccessmanager
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
QT implementation fillet window
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)