PAT1052


看题传送门 :https://www.patest.cn/contests/pat-b-practise/1052

  这个题很有意思的,用二维数组其实很难处理,主要原因是每个方括号内的字符数目其实是不确定的,这样导致很难储存,输出时也很难根据标号输出。而string容器和vector容器却能很好的解决这一问题,因为可以通过函数很方便的确定总符号数,而且在储存的时候也很好储存,一个string容器可以存储多个字符,这也为输出带来了方便。

  本题中使用了二维的vector数组,这样可以很好的区分手,眼,口,而且构建也很方便。

  下面上一波C艹实现

#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;

vector<vector<string> > ch;

int main()
{
    for(int i = 1;i <= 3;i++)
    {
        string cInput; // 临时保存输入
        getline(cin, cInput); // 由于输入中可能存在空格,所以使用getline函数读入
        vector<string> row; // 二维数组中的行,这也很方便啊,直接把本行所以输入处理后扔到vector中
        for(int j = 0;j < (int)cInput.length();j++)
        {
            if(cInput[j] == '[')
            {
                for(int k = j;k < (int)cInput.length();k++) // 嵌套循环直到找到结束的方括号
                {
                    if(cInput[k] == ']')
                    {
                        row.push_back( cInput.substr(j+1, k-j-1) ); //使用push_back函数很方便的将查询到的字串加入这一行
                        break;                                      //这一个字串就是一个或多个标号相同的字符
                    }
                }
            }
        }
        ch.push_back(row);//最后将这整个一行加入二维容器中
    }
    int n;
    scanf("%d",&n);
    int size_hand = ch[0].size();
    int size_eye = ch[1].size();
    int size_mou = ch[2].size();
    while(n--)
    {
        int a, b, c, d, e;
        cin >> a >> b >> c >> d >> e;
        if( a > size_hand || b > size_eye || c > size_mou || d > size_eye || e > size_hand || a < 1 || b < 1 || c < 1 || d < 1 || e < 1)
            printf("Are you kidding me? @\\/@\n"); //注意'\'是个转义字符,想要输出必须要\\才行
        else
        {
            cout << ch[0][a-1] << "(" << ch[1][b-1] << ch[2][c-1] << ch[1][d-1] << ")"<< ch[0][e-1] << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/aldo101/article/details/79650767