1. 程式人生 > >PAT-BASIC1052——賣個萌

PAT-BASIC1052——賣個萌

題目描述:

知識點:雜湊表

思路:用三個陣列分別記錄手、眼、口的表情

注意點:

(1)若使用者選擇的序號不存在,需要輸出"Are you kidding me? @\/@"。這裡面的"\"需要轉義輸出為"\\"。

(2)對於整行字串,最好用getline讀入,避免漏讀換行符。

時間複雜度和空間複雜度均是O(n1 + n2 + n3),其中n1、n2、n3分別為輸入第1、2、3行的字串長度。

C++程式碼:

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

using namespace std;

void generateArray(vector<string>& hands, string s); 

int main(){
	
	string hand;
	string eye;
	string mouth;
	
	getline(cin, hand);
	getline(cin, eye);
	getline(cin, mouth);
	
	vector<string> hands;
	vector<string> eyes;
	vector<string> mouths;
	
	generateArray(hands, hand);
	generateArray(eyes, eye);
	generateArray(mouths, mouth);
	
	int count;
	cin >> count;
	
	int leftHand;
	int leftEye;
	int midMouth;
	int rightEye;
	int rightHand;
	
	for(int i = 0; i < count; i++){
		cin >> leftHand >> leftEye >> midMouth >> rightEye >> rightHand;
		if(leftHand < 1 || leftHand > hands.size() || rightHand < 1 || rightHand > hands.size() || 
			leftEye < 1 || leftEye > eyes.size() || rightEye < 1 || rightEye > eyes.size() ||
			midMouth < 1 || midMouth > mouths.size()){
				printf("Are you kidding me? @\\/@\n");
			}else{
				cout << hands[leftHand - 1] << "(" << eyes[leftEye - 1] << mouths[midMouth - 1] << 
				eyes[rightEye - 1] << ")" << hands[rightHand - 1] << endl;
			}
	}
}

void generateArray(vector<string>& array, string s){
	string tempString;
	for(int i = 0; i < s.length(); i++){
		if(s[i] == '['){
			tempString = "";
		}else if(s[i] == ']'){
			array.push_back(tempString);
			tempString = "";
		}else{
			tempString += s[i];
		}
	}
}

C++解題報告: