傳送門
https://pintia.cn/problem-sets/994805260223102976/problems/994805273883951104
題目
萌萌噠表情符號通常由“手”、“眼”、“口”三個主要部分組成。簡單起見,我們假設(shè)一個表情符號是按下列格式輸出的:
[左手]([左眼][口][右眼])[右手]
現(xiàn)給出可選用的符號集合,請你按用戶的要求輸出表情。
輸入格式:
輸入首先在前三行順序?qū)?yīng)給出手、眼、口的可選符號集。每個符號括在一對方括號[]內(nèi)。題目保證每個集合都至少有一個符號,并不超過10個符號;每個符號包含1到4個非空字符。
之后一行給出一個正整數(shù)K,為用戶請求的個數(shù)。隨后K行,每行給出一個用戶的符號選擇,順序?yàn)樽笫?、左眼、口、右眼、右手——這里只給出符號在相應(yīng)集合中的序號(從1開始),數(shù)字間以空格分隔。
輸出格式:
對每個用戶請求,在一行中輸出生成的表情。若用戶選擇的序號不存在,則輸出“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? @/@
分析
先根據(jù)三行的表情,存入對應(yīng)的數(shù)組中,我用的是string數(shù)組,這里存數(shù)據(jù)是一個難點(diǎn),我是根據(jù)string.find方法找到中括號,然后用string.substr劃分?jǐn)?shù)組后,存入里面的。之后按要求輸出即可。
遇到的坑:
1.getline(cin,string)之后需要清空cin,方法如下
cin.sync();
cin.clear();
2.題目說“這里只給出符號在相應(yīng)集合中的序號(從1開始)”
3.別忘記給眼睛的左右兩邊加英文括號
所有的坑全踩了一遍,要不要這樣?!
源代碼
//C/C++實(shí)現(xiàn)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string hands, eyes, mouths;
vector<string> hands_v(1), eyes_v(1), mouths_v(1); //先放一個占位置
//input hands
int i = 0;
getline(cin, hands);
int end = hands.find(']', i);
while(end != -1){
int start = hands.find('[', i);
hands_v.push_back(hands.substr(start + 1, end - start - 1));
i = end + 1;
end = hands.find(']', i);
}
cin.sync();
cin.clear();
//input eyes
i = 0;
getline(cin, eyes);
end = eyes.find(']', i);
while(end != -1){
int start = eyes.find('[', i);
eyes_v.push_back(eyes.substr(start + 1, end - start - 1));
i = end + 1;
end = eyes.find(']', i);
}
cin.sync();
cin.clear();
//input mouths
i = 0;
getline(cin, mouths);
end = mouths.find(']', i);
while(end != -1){
int start = mouths.find('[', i);
mouths_v.push_back(mouths.substr(start + 1, end - start - 1));
i = end + 1;
end = mouths.find(']', i);
}
cin.sync();
cin.clear();
int n;
scanf("%d", &n);
int tmp;
for(int j = 0; j < n; ++j){
string result = "";
//左手
scanf("%d", &tmp);
if(tmp > 0 && tmp < hands_v.size()){
result += hands_v[tmp];
}
else{
printf("Are you kidding me? @\\/@\n");
continue;
}
//左眼
scanf("%d", &tmp);
if(tmp > 0 && tmp < eyes_v.size()){
result += ("(" + eyes_v[tmp]);
}
else{
printf("Are you kidding me? @\\/@\n");
continue;
}
//口
scanf("%d", &tmp);
if(tmp > 0 && tmp < mouths_v.size()){
result += mouths_v[tmp];
}
else{
printf("Are you kidding me? @\\/@\n");
continue;
}
//右眼
scanf("%d", &tmp);
if(tmp > 0 && tmp < eyes_v.size()){
result += (eyes_v[tmp] + ")");
}
else{
printf("Are you kidding me? @\\/@\n");
continue;
}
//右手
scanf("%d", &tmp);
if(tmp > 0 && tmp < hands_v.size()){
result += hands_v[tmp];
}
else{
printf("Are you kidding me? @\\/@\n");
continue;
}
cout << result << endl;
}
return 0;
}