1. 程式人生 > >PAT-BASIC1041——考試座位號

PAT-BASIC1041——考試座位號

題目描述:

知識點:字串

思路:開一個1001大小的陣列,試機座位號碼即陣列索引,陣列中儲存的值為考試座位號碼

本題是典型的空間換時間的思想。時間複雜度是O(1)。空間複雜度是O(1001)。

C++程式碼:

#include<iostream>
#include<string>

using namespace std;

struct student {
	string studentNumber;
	int tryNumber;
	int testNumber;
};

int main() {
	int totalNum;

	student tempStudent;
	string tempStudentNumber;
	int tempTryNumber;
	int tempTestNumber;

	cin >> totalNum;

	student students[1001];

	for (int i = 0; i < totalNum; i++) {
		cin >> tempStudentNumber >> tempTryNumber >> tempTestNumber;
		tempStudent.studentNumber = tempStudentNumber;
		tempStudent.tryNumber = tempTryNumber;
		tempStudent.testNumber = tempTestNumber;
		students[tempTryNumber] = tempStudent;
	}

	int queryNum;
	int queryNumber;
	
	cin >> queryNum;

	for (int i = 0; i < queryNum; i++) {
		cin >> queryNumber;
		cout << students[queryNumber].studentNumber << " " << students[queryNumber].testNumber << endl;
	}
}

C++解題報告: