2020年3月20日 星期五

2.2-Leaderboard

解題心得:
使用vector動態紀錄資料,全部輸入後使用getHighScores函數三次,此函數每次會找到該次最大值,輸出後把該質歸零。

程式碼:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

void getHighScores(vector<string> &names, vector<int> &scores)
{
 int maxScore = 0, mark = 0, i;
 for (i = 0; i < scores.size(); i++)
 {
  if (scores[i] > maxScore)
  {
   maxScore = scores[i];
   mark = i;
  }
 }
 cout << names[mark] << endl;
 cout << maxScore << endl;
 scores[mark] = 0;
}

int main()
{
 //fstream file;
 //file.open("scores.txt", ios::in);
 vector<int> scores;
 vector<string> names;
 string temp_name;
 int temp_score;
 while (cin >> temp_name >> temp_score) //read file
 {
  names.push_back(temp_name);
  scores.push_back(temp_score);

 }
 getHighScores(names, scores);
 getHighScores(names, scores);
 getHighScores(names, scores);
 //file.close();
 return 0;
}

沒有留言:

張貼留言