pta L1-054 福到了

细节很多,但是还是应该小心一点;

这道题的大体思路就是先存字符串,注意在输出完了n之后应该有一个getchar来吸收空格

然后输出完了之后把输入的字符串挨个存入另一个字符串数组,并将他们逆序,这样输出的时候就号输出了;

在判断是否是“福到了”的时候,可以引入一个判断变量flag,如果每个字符数组无论是逆序还是倒叙都相等,即下面这段代码

for(register int i=0;i<n;i++)
    {
        if(s1[i]==s[i])
        flag++;
        else 
        break;
    }

那么相等的字符串个数就加一,如果不相等,就停止这个的判断进入下一个判断,

根据条件,如果相等的字符串和总的字符串个数相等,就按正序输出先前输入的字符串数组s[i];

若不然,则按倒叙(注意,是按倒叙)输出逆序的字符数组即可,

其中,还有一些小细节,详情请看代码

Talk is cheap. Show me the code.

#include<bits/stdc++.h>
using namespace std;
int n;
int flag;
char ch;
string s1[110];
string s[110];
int main(){
    cin>>ch>>n;
    getchar();
    for(register int i=0;i<n;i++)
    {
        getline(cin,s[i]);
        s1[i]=s[i];//存入 
        reverse(s1[i].begin(),s1[i].end()); //反转 
    }
    for(register int i=0;i<n;i++)
    {
        if(s1[i]==s[i])
        flag++;//统计 
        else //及时止损 
        break;
    }
    if(flag==n)
    { 
        cout<<"bu yong dao le"<<endl;//输出 
        for(register int i=0;i<n;i++)//正序输出 
        {
            for(register int j=0;j<s[i].length();j++)
            {
                if(s[i][j]=='@')
                {
                    cout<<ch;
                }
                else 
                cout<<s[i][j];
            }
            if(i<n-1) //格式控制 
            cout<<endl;
        }    
    }
    else
    {
        for(register int i=n-1;i>=0;i--)//倒叙 
        {                
            for(register int j=0;j<s1[i].length();j++)//输出 
            {
                if(s1[i][j]=='@')
                    {
                        cout<<ch;
                    }
                else
                cout<<s1[i][j];
            }
            if(i>0)//格式控制 
            cout<<endl;    
        }
    }
} 

 

posted @ 2022-04-18 10:57  江上舟摇  阅读(162)  评论(0)    收藏  举报