当前位置:网站首页>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;
}
边栏推荐
- FLV格式详解
- (POJ - 1458) common subsequence (longest common subsequence)
- Codeforces Global Round 19
- SQL quick start
- Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
- Date plus 1 day
- Advancedinstaller installation package custom action open file
- 力扣leetcode第 280 场周赛
- (lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
- 解决Intel12代酷睿CPU单线程调度问题(二)
猜你喜欢
Raspberry pie 4B installation opencv3.4.0
第6章 Rebalance详解
<li>圆点样式 list-style-type
Installation and configuration of MariaDB
Chapter 1 overview of MapReduce
Remove the border when input is focused
Summary of game theory
The concept of spark independent cluster worker and executor
Tree of life (tree DP)
解决Intel12代酷睿CPU单线程只给小核运行的问题
随机推荐
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
Remove the border when input is focused
Research Report on market supply and demand and strategy of China's four seasons tent industry
It is forbidden to trigger onchange in antd upload beforeupload
(POJ - 1458) common subsequence (longest common subsequence)
JS encapsulates the method of array inversion -- Feng Hao's blog
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
解决Intel12代酷睿CPU单线程调度问题(二)
去掉input聚焦时的边框
(lightoj - 1369) answering queries (thinking)
Li Kou - 298th weekly match
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
LeetCode 1984. Minimum difference in student scores
Mp4 format details
Ffmpeg command line use
Calculate the time difference
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Codeforces Round #801 (Div. 2)A~C
LeetCode 1551. Minimum operand to make all elements in the array equal
Educational Codeforces Round 122 (Rated for Div. 2)