PAT B1052 卖个萌 (20 分)

在这里插入图片描述
字符串模拟题,主要考察了STL容器string和vector的使用,当然也可以用char和数组来实现,但是会非常麻烦。。。
如果出现段错误,注意一下输入序号不合理的判断(大于总长度或者小于1)

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

using namespace std;

vector<string> hand, eye, mouth;

void push(string s, vector<string> &v){
    
    
	for(int i=0; i<s.size(); i++){
    
    
		if(s[i] == '['){
    
    
			int j = i+1;
			while(s[j]!=']'){
    
    
				j++;
			}
			v.push_back(s.substr(i+1, j-i-1));
			i = j;
		}
	}
}

int main(){
    
    
	string h, e, m;
	getline(cin, h);getline(cin, e);getline(cin, m);
	push(h, hand);
	push(e, eye);  
	push(m, mouth);
	
	int n;
	int lh, le, mm, re, rh;
	scanf("%d", &n);
	
	for(int i=0; i<n; i++){
    
    
		scanf("%d %d %d %d %d", &lh, &le, &mm, &re, &rh);
		if(lh>hand.size() || le>eye.size() || mm>mouth.size() || re>eye.size() || rh>hand.size() || lh<1 || le<1 || mm<1 || re<1 || rh<1){
    
    
			printf("Are you kidding me? @\\/@\n");
			continue;
		}
		cout << hand[lh-1] << '(' << eye[le-1] << mouth[mm-1] << eye[re-1] << ')' << hand[rh-1] << endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45964844/article/details/113661340