PAT Basic 1052
阿新 • • 發佈:2018-08-12
include 序號 als 需要 還需要 ret clas 題解 scanf
1052 賣個萌
萌萌噠表情符號通常由“手”、“眼”、“口”三個主要部分組成。簡單起見,我們假設一個表情符號是按下列格式輸出的:
[左手]([左眼][口][右眼])[右手]
現給出可選用的符號集合,請你按用戶的要求輸出表情。
輸入格式:
輸入首先在前三行順序對應給出手、眼、口的可選符號集。每個符號括在一對方括號 []
內。題目保證每個集合都至少有一個符號,並不超過 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? @\/@
題解:我發現這幾道題是真的坑,這道題主要有兩個坑點:
①"Are you kidding me? @\/@"需要寫成"Are you kidding me? @\\/@",因為\是轉義符號。
②輸出"Are you kidding me? @\\/@"時,用戶給出的不一定是大於這個數據的數據量,也有可能是0或負數
這道題還需要註意處理輸入的問題,用判斷[的方式來輸入我個人覺得是一個比較好的方法。
代碼如下:
1 #include<iostream> 2 #include<string> 3 4 using namespace std; 5 6 int get( string temp, string a[]) 7 { 8 int k = 0; 9 for( int i = 0; i < temp.length(); i++){ 10 if( temp[i] == ‘[‘){ 11 i++; 12 while(temp[i] != ‘]‘){ 13 a[k] += temp[i];14 i++; 15 } 16 k++; 17 } 18 } 19 return k; 20 } 21 22 int main() 23 { 24 int k, temp[5]; 25 string mouth, eye, hand; 26 string a[15], b[15], c[15]; 27 getline( cin, hand); 28 getline( cin, eye); 29 getline( cin, mouth); 30 int len1 = get( hand, a); 31 int len2 = get( eye, b); 32 int len3 = get( mouth, c); 33 34 scanf("%d",&k); 35 while(k--){ 36 bool temp3 = true; 37 for( int i = 0; i < 5; i++){ 38 scanf("%d",&temp[i]); 39 } 40 if( temp[0] > len1 || temp[0] < 1) 41 temp3 = false; 42 else if( temp[1] > len2 || temp[1] < 1) 43 temp3 = false; 44 else if( temp[2] > len3 || temp[2] < 1) 45 temp3 = false; 46 else if( temp[3] > len2 || temp[3] < 1) 47 temp3 = false; 48 else if( temp[4] > len1 || temp[4] < 1) 49 temp3 = false; 50 if( !temp3){ 51 cout << "Are you kidding me? @\\/@" << endl; 52 continue; 53 } 54 else{ 55 cout<<a[temp[0]-1]<<"("<<b[temp[1]-1]<<c[temp[2]-1]<<b[temp[3]-1]<<")"<<a[temp[4]-1]<<endl; 56 } 57 } 58 return 0; 59 }
PAT Basic 1052