当前位置:网站首页>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;
}











原网站

版权声明
本文为[HBUcs2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131314584152.html