PAT乙级—1052 卖个萌 (20分)

萌萌哒表情符号通常由“手”、“眼”、“口”三个主要部分组成。简单起见,我们假设一个表情符号是按下列格式输出的:

[左手]([左眼][][右眼])[右手]

现给出可选用的符号集合,请你按用户的要求输出表情。
输入格式:
输入首先在前三行顺序对应给出手、眼、口的可选符号集。每个符号括在一对方括号 []内。题目保证每个集合都至少有一个符号,并不超过 10 个符号;每个符号包含 1 到 4 个非空字符。
之后一行给出一个正整数 K,为用户请求的个数。随后 K 行,每行给出一个用户的符号选择,顺序为左手、左眼、口、右眼、右手——这里只给出符号在相应集合中的序号(从 1 开始),数字间以空格分隔。
输出格式:
对每个用户请求,在一行中输出生成的表情。若用户选择的序号不存在,则输出 Are you kidding me? @\/@

输入样例:

[][][o][~\][/~]  [<][>]
[][][^][-][=][>][<][@][]
[Д][][_][ε][^]  ...
4
1 1 2 2 2
6 8 1 5 5
3 3 4 3 3
2 10 3 9 3

输出样例:

(╯▽╰)<(=)/~
o(^ε^)o
Are you kidding me? @\/@

思路:
  用最笨拙的方式搜寻字符串中的内容,然后按位置遍历即可

注意:

  1. 题目输出中加了括号,说明在表情中已经没有转义字符的情况了
  2. C++的查找切割,用find()就行了
  3. @/@连在一起输出,需要转义@/@
  4. 选择的数,可能大于符号数,也可以小于1
  5. C++函数可以直接传递索引&

代码:(C++)

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
	string a,b,c;
	vector<string> hand;
	vector<string> eye;
	vector<string> mouse;
	
	getline(cin,a);
	getline(cin,b);
	getline(cin,c);

	int first;
	int last;
	while(a.find_first_of('[') != a.npos)
	{
		first = a.find_first_of('[');
		last = a.find_first_of(']');
		hand.push_back(a.substr(first+1,last-first-1));
		a = a.substr(last+1);
	}
	while(b.find_first_of('[') != b.npos)
	{
		first = b.find_first_of('[');
		last = b.find_first_of(']');
		eye.push_back(b.substr(first+1,last-first-1));
		b = b.substr(last+1);
	}
	while(c.find_first_of('[') != c.npos)
	{
		first = c.find_first_of('[');
		last = c.find_first_of(']');
		mouse.push_back(c.substr(first+1,last-first-1));
		c = c.substr(last+1);
	}
	int n;
	cin>>n;
	string str[n];
	for(int i=0; i<n; i++)
		str[i] = "";
	int an[n][5];
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<5; j++)
			cin>>an[i][j];
	}
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<5; j++)
		{
			bool flag = false;
			switch(j)
			{
				case 0:{
					if(an[i][j]>hand.size() || an[i][j]<=0)
					{
						flag = true;
					}
					else
					{
						str[i]+=hand[an[i][j]-1];
						if(j==0)
							str[i]+='(';
					}
						
					break;
				}
				case 1:{
					if(an[i][j]>eye.size() || an[i][j]<=0)
					{
						flag = true;
					}
					else
					{
						str[i]+=eye[an[i][j]-1];
						if(j==3)
							str[i]+=')';
					}
						
					break;
				}
				case 2:{
					if(an[i][j]>mouse.size() || an[i][j]<=0)
					{
						flag = true;
					}
					else
						str[i]+=mouse[an[i][j]-1];
					break;
				}
				case 3:{
					if(an[i][j]>eye.size() || an[i][j]<=0)
					{
						flag = true;
					}
					else
					{
						str[i]+=eye[an[i][j]-1];
						str[i]+=')';
					}

					break;
				}
				case 4:{
					if(an[i][j]>hand.size() || an[i][j]<=0)
					{
						flag = true;
					}
					else
					{
						str[i]+=hand[an[i][j]-1];
					}
					break;
				}

			}
			if(flag)
			{
				str[i]="";
				str[i]+="Are you kidding me? @\\/@";
				break;
			}
				
		}
	}
	for(int i=0; i<n; i++)
		cout<<str[i]<<endl;
	
    return 0;
}

代码可以优化的地方:
参考:https://www.cnblogs.com/littlepage/p/11611962.html

  1. 读取符号的地方可以写成一个函数,能有效减少代码
  2. 对于是否不存在这个位置,可以用一个if提前判断,不用写那么多switch
  3. 可以逐行输出结果,不必要最后一次输出
#include <iostream>
#include <vector>

using namespace std;

void analyse_data(vector<string>& vec,string& str){
    for(int i=0;i<str.length();i++){
        if(str[i]=='['){
            int start=i+1;
            while(str[i]!=']'){
                i++;
            }
            int end=i;
            vec.push_back(str.substr(start,end-start));
        }
    }
}

int main()
{
    string hands,eyes,months;
    vector<string> vec_hands,vec_eyes,vec_months;
    getline(cin,hands);
    getline(cin,eyes);
    getline(cin,months);
    analyse_data(vec_hands,hands);
    analyse_data(vec_eyes,eyes);
    analyse_data(vec_months,months);
    int testnum;
    cin>>testnum;
    while(testnum--){
        int a,b,c,d,e;
        cin>>a>>b>>c>>d>>e;
        if(a<=0||b<=0||c<=0||d<=0||e<=0||
           a>vec_hands.size()||b>vec_eyes.size()||c>vec_months.size()||
           d>vec_eyes.size()||e>vec_hands.size()){
            cout<<"Are you kidding me? @\\/@"<<endl;
            continue;
        }
        cout<<vec_hands[a-1]<<"("<<vec_eyes[b-1]<<vec_months[c-1]<<vec_eyes[d-1]<<")"<<vec_hands[e-1]<<endl;
    }
    system("pause");
    return 0;
}
发布了77 篇原创文章 · 获赞 20 · 访问量 5773

猜你喜欢

转载自blog.csdn.net/qq_42396168/article/details/105024779
今日推荐