【PAT乙级】1052 卖个萌

题目链接:1052 卖个萌

思路

  1. getline输入三行,另写一个函数用于添加串内符号,判断至第n'['出现,将后续内容加入输出,直到']'结束加入。
  2. 注意错误输出中的'\'为转义符,无法单独输出,需改为"\\"

代码

#include <iostream>
#include <math.h>
using namespace std;

//输入选串及序号,将对应字符加入输出,成功返回1,失败返回0
int Select(string &out,string s,int no){
    int count = 0;
    for(int i=0;i<s.length();i++){
        if(s[i] == '[')count++;
        if(count == no){
            while(s[++i]!=']') out += s[i];
            return 1;
        }
    }
    return 0;
}

int main(){
    string hand,eye,mouth;
    getline(cin,hand);
    getline(cin,eye);
    getline(cin,mouth);
    int N,no,flag=1;
    cin >> N;
    string out[N];
    for(int i=0;i<N;i++){
        for(int j=0;j<5;j++){
            cin >> no;
            if(j==0||j==4) flag = Select(out[i], hand, no);
            if(j==1) out[i]+='(';
            if(j==1||j==3) flag = Select(out[i], eye, no);
            if(j==3) out[i]+=')';
            if(j==2) flag = Select(out[i], mouth, no);
            if (!flag){
                out[i]="Are you kidding me? @\\/@";
                break;
            }
        }
        cout << out[i] << endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wulingyu501/article/details/108932868
今日推荐