【PAT乙级】1052 卖个萌

    科技2022-09-02  100

    题目链接:1052 卖个萌

    思路

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

    代码

    #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; }

     

    Processed: 0.008, SQL: 9