vector与struct实现
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std
;
struct Project
{
int num
;
int score
=0;
bool operator == (const int &fake_num
);
};
inline bool Project
::operator ==(const int &fake_num
) {
return(this->num
== fake_num
);
}
inline bool cmp(Project
&x
, Project
&y
) {
return x
.score
< y
.score
;
}
int main() {
int N
;
cin
>> N
;
vector
<Project
>A
;
Project temporary
;
for (int i
= 0; i
< N
; i
++) {
int fake_num
;
int fake_score
;
cin
>> fake_num
>> fake_score
;
vector
<Project
>::iterator it
= find(A
.begin(), A
.end(), fake_num
);
if (it
!= A
.end()) {
A
[int(&*it
- &A
[0])].score
+= fake_score
;
}
else {
temporary
.num
= fake_num
;
temporary
.score
= fake_score
;
A
.push_back(temporary
);
}
}
temporary
=*max_element(A
.begin(), A
.end(), cmp
);
cout
<< temporary
.num
<< ' ' << temporary
.score
;
system("pause");
return 0;
}
1.最后的测试点想不到办法解决,欢迎评论交流、 2.我有其他的写法,但刚接触vector不久,想通过这题锻炼一下。 3.不足请指出,请各位大佬指教。
转载请注明原文地址: https://mac.8miu.com/read-509813.html