Re: [問題] ITSA第24次第4題

看板C_and_CPP作者 (師大狗鼻哥)時間11年前發表 (2014/05/05 12:02), 11年前編輯推噓0(004)
留言4則, 2人參與, 最新討論串4/4 (看更多)
※ 引述《ACMANIAC ()()》之銘言: : 關於排序,這樣寫可能會更省時省力,尤其在程式競賽。 : #include <iostream> : #include <algorithm> : #include <string> : #include <vector> : using namespace std; : struct MVP { : string name; : int salary, people; : MVP(const string &name, const int &salary, const int &people) { : this->name = name; : this->salary = salary; : this->people = people; : } : bool operator < (const MVP &m) const : { : return this->salary > m.salary || : this->salary == m.salary && this->people > m.people; : } : }; : int main() : { : string name; : int N, S, P; : cin >> N; : vector<MVP> m; : for (int i = 0; i < N && cin >> name >> S >> P; ++i) { : m.push_back(MVP(name, S, P)); : } : sort(m.begin(), m.end()); : for (int i = 0; i < N; ++i) { : if (i) { : cout << " "; : } : cout << m[i].name; : } : cout << endl; : return 0; : } : ※ 引述《kkkmode (kkk)》之銘言: : : 我拿你的code去編譯 : : 發現要加#include<string.h> 或 include<cstring>才會編過 : : <cName>是在C++引用C的header<Name.h>的習慣 : : 這題有兩個重點: : : 1.先排序不重要的index再排序重要的 : : 2.如何用標準庫排序自定義類型 : : 以下是我改寫的code,可以參考一下 : : #include <iostream> : : #include <vector> : : #include <algorithm> : : #include <functional> : : #include <string> : : using namespace std; : : struct playerInfo : : { : : int salary; : : int fans; : : string name; : : }; : : bool by_salary(const playerInfo& p1,const playerInfo& p2) : : { : : return p1.salary > p2.salary; : : } : : bool by_fans(const playerInfo& p1,const playerInfo& p2) : : { : : return p1.fans > p2.fans; : : } : : int main(){ : : vector<playerInfo > playerList; : : unsigned int size=0; : : cin>>size; : : for(int i=0;i<size;i++) //input player's information : : { : : playerInfo temp; : : cin>>temp.name>>temp.salary>>temp.fans; : : playerList.push_back(temp); : : } : : sort(playerList.begin(),playerList.end(),by_fans); : : sort(playerList.begin(),playerList.end(),by_salary); : : for(int i=0;i<size;i++) //output player's information : : cout<<playerList[i].name<< (i<size-1 ? ' ':'\n'); : : return 0; : : } #include <iostream> #include <algorithm> #include <string> #include <vector> #include <iterator> using namespace std; struct MVP { string name; int salary, people; inline bool operator <(const MVP &m) const { return this->salary > m.salary || (this->salary == m.salary && this->people > m.people); } }; inline istream& operator>> (istream& in, MVP& mvp) { return in >> mvp.name >> mvp.salary >> mvp.people; } int main() { int N; cin >> N; istream_iterator<MVP> iiter(cin),eos; vector<MVP> m(iiter,eos); sort(m.begin(), m.end()); for (int i = 0; i < N; ++i) cout<< char(!!i * ' ') << m[i].name; cout << endl; return 0; } 幫忙簡化一下 不過有些地方是懶人偷吃步 盡量少用... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.122.216.115 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1399291371.A.130.html ※ 編輯: soheadsome (140.122.216.115), 05/05/2014 20:05:07

05/05 20:33, , 1F
連 !! 都出現了 XDD
05/05 20:33, 1F

05/05 20:39, , 2F
我不懂 <iterator>,想請教一下,這篇的用法
05/05 20:39, 2F

05/05 20:39, , 3F
是無視題目給的 N 筆資料嗎?如果加進去要如何寫?謝謝
05/05 20:39, 3F

05/05 21:57, , 4F
加進去就用上一篇的做法吧...
05/05 21:57, 4F
對 是直接無視N 通常OJ系統 都是用IO轉向 所以只要讀到EOF 輸入就結束了 所以基本上不需要去計數 但是輸出就要 ※ 編輯: soheadsome (140.122.216.115), 05/05/2014 21:59:21 istream_iterator<MVP> iiter(cin); 宣告一個istream_iterator 他的資料來源是從cin 每次讀入的型別為MVP(記得自訂型別要overload) 當然 你如果有宣告一個ifstream fin;也可以這樣用 istream_iterator<MVP> eos; 是宣告istream_iterator的結束 iiter類似容器的 .begin() eos則類似 .end() 所以我也可以這樣用: copy(iiter,eos,back_inserter(m)); 把資料塞到m這個容器的後面 ※ 編輯: soheadsome (140.122.216.115), 05/05/2014 22:07:01 這就是STL相當好用的地方 這也是我很喜歡c++的一個部分XD ※ 編輯: soheadsome (140.122.216.115), 05/05/2014 22:08:09
文章代碼(AID): #1JPtth4m (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):
問題
1
6
文章代碼(AID): #1JPtth4m (C_and_CPP)