第一条题解
2025-11-28 19:25:49
发布于:山东
14阅读
0回复
0点赞
题目分析 :
这道题我们可以用三个栈来存储最擅长编程数学和体育的人,w = min(擅长编程人数,擅长数学人数,擅长体育人数),最后输出每一组即可。
伪代码(输入部分不予展示):
/*
stack<int> t1, t2, t3
for 1 to n :
if x == 1 : x -> t1
else if x == 2 : x -> t2
else x -> t3
min(len(t1),len(t2),len(t3)) -> w
for 1 to w :
print ...
pop ...
*/
#include<bits/stdc++.h>
using namespace std;
int n, w;
stack<int> program, math, PE;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
int x;
cin >> x;
if(x == 1) program.push(i);
else if(x == 2) math.push(i);
else PE.push(i);
}
w = min({program.size(), math.size(), PE.size()});
cout << w << endl;
while(w--) {
cout << program.top() << ' ' << math.top() << ' ' << PE.top() << endl;
program.pop(), math.pop(), PE.pop();
}
return 0;
}
这里空空如也

有帮助,赞一个