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

看板C_and_CPP作者 ()()時間11年前 (2014/05/04 20:59), 編輯推噓2(202)
留言4則, 3人參與, 最新討論串3/4 (看更多)
關於排序,這樣寫可能會更省時省力,尤其在程式競賽。 #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; : } : ※ 引述《ga544523 (美麗新世界)》之銘言: : : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : : c++ : : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : : iostream : : 問題(Question): : : http://truth.bahamut.com.tw/s01/201405/2827e54fa6e7853d0a80ffab1381d289.JPG
: : 錯誤結果(Wrong Output): : : http://truth.bahamut.com.tw/s01/201405/08372dfa3afb99c7500459ef7f41b9e9.JPG
: : 程式碼(Code):(請善用置底文網頁, 記得排版) : : #include<iostream> : : using namespace std; : : int main(){ : : char (*a)[50] = new char[100000][50]; : : char (*tmp)[50] = new char[100000][50]; : : long long (*b)= new long long[100000]; : : long long (*c)= new long long[100000]; : : long long d; : : cin>>d; : : for(int i=0;i<d;i++){ : : cin>>a[i]; : : cin>>b[i]; : : cin>>c[i]; : : } : : for(int j=0;j<d-1;j++){ : : for(int i=0;i<d;i++){ : : if(b[i]<b[i+1]){ : : strcpy(tmp[i],a[i]); : : strcpy(a[i],a[i+1]); : : strcpy(a[i+1],tmp[i]); : : swap(b[i],b[i+1]); : : swap(c[i],c[i+1]); : : } : : if(b[i]==b[i+1]&&c[i]<c[i+1]){ : : strcpy(tmp[i],a[i]); : : strcpy(a[i],a[i+1]); : : strcpy(a[i+1],tmp[i]); : : swap(b[i],b[i+1]); : : swap(c[i],c[i+1]);}}} : : for(int i=0;i<d;i++){ : : cout<<a[i]<<" ";} : : delete [] a; : : delete [] tmp; : : delete [] b; : : delete [] c; : : return 0;} : : 補充說明(Supplement): : : 這是我的執行畫面 : : http://truth.bahamut.com.tw/s01/201405/5baabcea5be42fd24b659947cde82090.JPG
: : 是我搞錯題目意思還是哪裡打錯了嗎 : : 測試沒問題阿(應該巴.) : : 傳上去卻一直說錯誤 : : ptt好難用喔 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.66.250.29 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1399237172.A.C7C.html

05/05 21:34, , 1F
這是一個不錯的作法
05/05 21:34, 1F

05/05 22:11, , 2F
m.push_back(MVP(name, S, P)); 在c++11的話
05/05 22:11, 2F

05/05 22:11, , 3F
可以改成m.emplace_back(name, S, P);
05/05 22:11, 3F

05/06 02:02, , 4F
程式競賽的 compiler 非常落後的 XD 通常都是 GCC 3.3.X
05/06 02:02, 4F
文章代碼(AID): #1JPgeqny (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
以下文章回應了本文
完整討論串 (本文為第 3 之 4 篇):
問題
1
6
文章代碼(AID): #1JPgeqny (C_and_CPP)