PAT乙 1052. 卖个萌

题目地址:1052.卖个萌

易错分析:

1.    需要考虑表情长度可能超过1

2.    输入下标需要判断,不仅要小于可用表情长度,还要检验是否小于1

程序:

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


// 在判断输入的下标是否合理时,不但要检验是否超过表情向量长度,还要检验是否小于1
int main()
{
    vector<vector<string> > v;  // 创建2维向量
    string s;
    vector<int> lenExpression;  
    
    for (int i = 0; i < 3; i++)
    {
    	vector<string> row;
    	getline(cin, s);   // 获得一行
    	int j = 0, m;
    	int len = s.length();
    	int counter = 0;
    	while (j < len)
    	{
    		if (s[j] == '[')
    		{
    			m = j + 1;
    			while (m++ < len)
    			{
    				if (s[m] == ']')    // 用于处理长度大于1的表情
    				{
    					row.push_back(s.substr(j+1, m-j-1));
    					counter++;
    					break;
    				}
    			}	
    		}
    		j++; 
    	}
    	lenExpression.push_back(counter);  // 保存每行表情的个数  
    	v.push_back(row);	    	
    }

    int K;
    cin >> K;
    int a, b, c, d, e;
    while (K--)
    {
    	cin >> a >> b >> c >> d >> e;
    	if (a > lenExpression[0] || b > lenExpression[1] || c > lenExpression[2] || 
    		d > lenExpression[1] || e > lenExpression[0] || a < 1 || b < 1 ||
    		c < 1 || d < 1 || e < 1)
    	{
    		cout << "Are you kidding me? @\\/@" << endl;  // \需要转义符\
    		continue;
    	}
    	else
    	{
    		cout << v[0][a-1] << "(" << v[1][b-1] << v[2][c-1] << v[1][d-1] << ")" << v[0][e-1] << endl;
    	}

    }


    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值