天天看點

PAT練習筆記——3.2 查找元素第3章 入門篇(1)——入門模拟

2019年9月PAT - 練習筆記——3.2

以下頁碼标注的是閱讀器中實際頁碼,而不是書本身自印的頁碼。

第3章 入門篇(1)——入門模拟

3.2 查找元素

目錄

  1. B1041 考試座位号
  2. B1004 成績排名
  3. B1028 人口普查
  4. B1032 挖掘機技術哪家強
  5. A1011 World Cup Betting
  6. A1006 Sign In and Sign Out
  7. A1036 Boys VS Girls
  1. B1041 考試座位号

    每個 PAT 考生在參加考試時都會被配置設定兩個座位号,一個是試機座位,一個是考試座位。正常情況下,考生在入場時先得到試機座位号碼,入座進入試機狀态後,系統會顯示該考生的考試座位号碼,考試時考生需要換到考試座位就座。但有些考生遲到了,試機已經結束,他們隻能拿着領到的試機座位号碼求助于你,從背景查出他們的考試座位号碼。

    輸入格式:

    輸入第一行給出一個正整數 N(≤1000),随後 N 行,每行給出一個考生的資訊:

    準考證号 試機座位号 考試座位号

    。其中

    準考證号

    由 16 位數字組成,座位從 1 到 N 編号。輸入保證每個人的準考證号都不同,并且任何時候都不會把兩個人配置設定到同一個座位上。

    考生資訊之後,給出一個正整數 M(≤N),随後一行中給出 M 個待查詢的試機座位号碼,以空格分隔。

    輸出格式:

    對應每個需要查詢的試機座位号碼,在一行中輸出對應考生的準考證号和考試座位号碼,中間用 1 個空格分隔。

    輸入樣例:

    4
    3310120150912233 2 4
    3310120150912119 4 1
    3310120150912126 1 3
    3310120150912002 3 2
    2
    3 4
               

    輸出樣例:

    3310120150912002 2
               
    1. 我的
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	string numbers[1001];
      	int seats[1001];
      	for (int i = 0;i < n;++i) {
      		string number = "";
      		int seat1 = 0, seat2 = 0;
      		cin >> number >> seat1 >> seat2;
      		
      		numbers[seat1] = number;
      		seats[seat1] = seat2;
      	}
      	
      	int m = 0;
      	cin >> m;
      	for (int i = 0;i < m;++i) {
      		int seat = 0;
      		cin >> seat;
      		cout << numbers[seat] << " " << seats[seat] << endl;
      	}
      	
      	return 0;
       }
                 
    2. 《算法筆記》P37

      可以用long long存準考證号

      可以用結構體把準考證号和考試座位号存一起

  2. B1004 成績排名

    讀入 n(>0)名學生的姓名、學号、成績,分别輸出成績最高和成績最低學生的姓名和學号。

    輸入格式:

    每個測試輸入包含 1 個測試用例,格式為
    第 1 行:正整數 n
    第 2 行:第 1 個學生的姓名 學号 成績
    第 3 行:第 2 個學生的姓名 學号 成績
      ... ... ...
    第 n+1 行:第 n 個學生的姓名 學号 成績
               
    其中

    姓名

    學号

    均為不超過 10 個字元的字元串,成績為 0 到 100 之間的一個整數,這裡保證在一組測試用例中沒有兩個學生的成績是相同的。

    輸出格式:

    對每個測試用例輸出 2 行,第 1 行是成績最高學生的姓名和學号,第 2 行是成績最低學生的姓名和學号,字元串間有 1 空格。

    輸入樣例:

    3
    Joe Math990112 89
    Mike CS991301 100
    Mary EE990830 95
               

    輸出樣例:

    Mike CS991301
    Joe Math990112
               
    1. 我的
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	int max = -1, min = 101;
      	string maxName = "", minName = "";
      	string maxNumber = "", minNumber = "";
      	for (int i = 0;i < n;++i) {
      		string name = "";
      		string number = "";
      		int score = 0;
      		cin >> name >> number >> score;
      		
      		if (score > max) {
      			max = score;
      			maxName = name;
      			maxNumber = number;
      		}
      		if (score < min) {
      			min = score;
      			minName = name;
      			minNumber = number;
      		}
      	}
      	cout << maxName << " " << maxNumber << endl;
      	cout << minName << " " << minNumber << endl;
      	
      	return 0;
       }
                 
    2. 《算法筆記》P38
  3. B1028 人口普查

    某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程式,找出鎮上最年長和最年輕的人。

    這裡確定每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過 200 歲的老人,而今天是 2014 年 9 月 6 日,是以超過 200 歲的生日和未出生的生日都是不合理的,應該被過濾掉。

    輸入格式:

    輸入在第一行給出正整數 N,取值在(0,105];随後 N 行,每行給出 1 個人的姓名(由不超過 5 個英文字母組成的字元串)、以及按

    yyyy/mm/dd

    (即年/月/日)格式給出的生日。題目保證最年長和最年輕的人沒有并列。

    輸出格式:

    在一行中順序輸出有效生日的個數、最年長人和最年輕人的姓名,其間以空格分隔。

    輸入樣例:

    5
    John 2001/05/12
    Tom 1814/09/06
    Ann 2121/01/30
    James 1814/09/05
    Steve 1967/11/20
               

    輸出樣例:

    3 Tom John
               
    1. 我的
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	int count = 0;
      	string oldest = "", youngest = "";
      	string max = "2014/09/06", min = "1814/09/06";
      	string maxNow = "1814/09/05", minNow = "2014/09/07";
      	for (int i = 0;i < n;++i) {
      		string name = "", date = "";
      		cin >> name >> date;
      		
      		if (min <= date && date <= max) {
      			++count;
      			if (date > maxNow) {
      				maxNow = date;
      				oldest = name;
      			}
      			if (date < minNow) {
      				minNow = date;
      				youngest = name;
      			}
      		}
      	}
          if (count) cout << count << " " << youngest << " " << oldest;
          else cout << 0;
      	
      	return 0;
       }
                 
    2. 《算法筆記》P41

      參考代碼用int分别存年月日,我覺得麻煩了

  4. B1032 挖掘機技術哪家強

    為了用事實說明挖掘機技術到底哪家強,PAT 組織了一場挖掘機技能大賽。現請你根據比賽結果統計出技術最強的那個學校。

    輸入格式:

    輸入在第 1 行給出不超過 105 的正整數 N,即參賽人數。随後 N 行,每行給出一位參賽者的資訊和成績,包括其所代表的學校的編号(從 1 開始連續編号)、及其比賽成績(百分制),中間以空格分隔。

    輸出格式:

    在一行中給出總得分最高的學校的編号、及其總分,中間以空格分隔。題目保證答案唯一,沒有并列。

    輸入樣例:

    6
    3 65
    2 80
    1 100
    2 70
    3 40
    3 0
               

    輸出樣例:

    2 150
               
    1. 我的
      #include <iostream>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	int maxNumber = 0;
      	int scores[100001] = {0};
      	for (int i = 0;i < n;++i) {
      		int number = 0, score = 0;
      		cin >> number >> score;
      		scores[number] += score;
      		
      		if (scores[number] > scores[maxNumber])	maxNumber = number;
      	}
      	cout << maxNumber << " " << scores[maxNumber];
      	
      	return 0;
       }
                 
    2. 《算法筆記》P42
  5. A1011 World Cup Betting

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

    Chinese Football Lottery provided a “Triple Winning” game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results – namely

    W

    for win,

    T

    for tie, and

    L

    for lose. There was an odd assigned to each result. The winner’s odd would be the product of the three odds times 65%.

    For example, 3 games’ odds are given as the following:

    W    T    L
    1.1  2.5  1.7
    1.2  3.1  1.6
    4.1  1.2  1.1
               
    To obtain the maximum profit, one must buy

    W

    for the 3rd game,

    T

    for the 2nd game, and

    T

    for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1×3.1×2.5×65%−1)×2=39.31 yuans (accurate up to 2 decimal places).

    Input Specification:

    Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to

    W

    ,

    T

    and

    L

    .

    Output Specification:

    For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

    Sample Input:

    1.1 2.5 1.7
    1.2 3.1 1.6
    4.1 1.2 1.1
               

    Sample Output:

    T T W 39.31
               
    1. 我的
      #include <iostream>
      #include <iomanip>
      
      using namespace std;
      
      int main(void)
      {
      	float profit = 1;
      	for (int i = 0;i < 3;++i) {
      		float w = 0, t = 0, lo = 0;
      		cin >> w >> t >> lo;
      		
      		float odd = w;
      		char bet = 'W';
      		if (t >= w && t >= lo) {
      			odd = t;
      			bet = 'T';
      		}
      		if (lo >= w && lo >= t) {
      			odd = lo;
      			bet = 'L';
      		}
      		profit *= odd;
      		cout << bet << " "; 
      	}
      	cout << setiosflags(ios::fixed) << setprecision(2) << (profit * 0.65 - 1) * 2;
      	
      	return 0;
       }
                 
    2. 《算法筆記》P44
  6. A1006 Sign In and Sign Out

    At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in’s and out’s, you are supposed to find the ones who have unlocked and locked the door on that day.

    Input Specification:

    Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format
    ID_number Sign_in_time Sign_out_time
               
    where times are given in the format

    HH:MM:SS

    , and

    ID_number

    is a string with no more than 15 characters.

    Output Specification:

    For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

    Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

    Sample Input:

    3
    CS301111 15:30:28 17:00:10
    SC3021234 08:00:00 11:25:25
    CS301133 21:45:00 21:58:40
               

    Sample Output:

    SC3021234 CS301133
               
    1. 我的
      #include <iostream>
      
      using namespace std;
      
      int main(void)
      {
      	int m = 0;
      	cin >> m;
      	
      	string min = "24:00:00", max = "";
      	string minID = "", maxID = "";
      	for (int i = 0;i < m;++i) {
      		string ID = "", in = "", out = "";
      		cin >> ID >> in >> out;
      		
      		if (in < min) {
      			min = in;
      			minID = ID;
      		}
      		if (out > max) {
      			max = out;
      			maxID = ID;
      		}
      	}
      	cout << minID << " " << maxID;
      		
      	return 0;
       }
                 
    2. 《算法筆記》P46

      參考代碼用int存時間,我覺得麻煩了

  7. A1036 Boys VS Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

    Input Specification:

    Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s

    name

    ,

    gender

    ,

    ID

    and

    grade

    , separated by a space, where

    name

    and

    ID

    are strings of no more than 10 characters with no space,

    gender

    is either

    F

    (female) or

    M

    (male), and

    grade

    is an integer between 0 and 100. It is guaranteed that all the grades are distinct.

    Output Specification:

    For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF−gradeM. If one such kind of student is missing, output

    Absent

    in the corresponding line, and output

    NA

    in the third line instead.

    Sample Input 1:

    3
    Joe M Math990112 89
    Mike M CS991301 100
    Mary F EE990830 95
               

    Sample Output 1:

    Mary EE990830
    Joe Math990112
    6
               

    Sample Input 2:

    1
    Jean M AA980920 60
               

    Sample Output 2:

    Absent
    Jean AA980920
    NA
               
    1. 我的
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(void)
      {
      	int n = 0;
      	cin >> n;
      	
      	int min = 101, max = -1;
      	string minName = "", maxName = "";
      	string minID = "", maxID = "";
      	for (int i = 0;i < n;++i) {
      		string name = "", ID = "";
      		char gender = 0;
      		int grade = 0;
      		cin >> name >> gender >> ID >> grade;
      		
      		if (gender == 'F' && grade > max) {
      			max = grade;
      			maxName = name;
      			maxID = ID;
      		}
      		else if (gender == 'M' && grade < min) {
      			min = grade;
      			minName = name;
      			minID = ID;
      		}
      	}
      	if (minName != "" && maxName != "") cout << maxName << " " << maxID << endl << minName << " " << minID << endl << max - min;
      	else {
      		if (maxName == "") cout << "Absent" << endl;
      		else cout << maxName << " " << maxID << endl;
      		
      		if (minName == "") cout << "Absent" << endl;
      		else cout << minName << " " << minID << endl;
              cout << "NA";
      	}
      	
      	return 0;
       }
                 
    2. 《算法筆記》P48