XYC的Ship @ 2019-08-16 17:20:18
大水题,真不明白你们怎么错的
#include<bits/stdc++.h>
using namespace std;
int a[301],b[301],c[301],d[301],e[301];
int main()
{
int n,i,j;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i]>>b[i]>>c[i];
}
for(i=1;i<=n;i++)
{
d[i]=a[i]+b[i]+c[i];
e[i]=i;
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(d[j]<d[j+1])
{
swap(d[j],d[j+1]);
swap(e[j],e[j+1]);
swap(a[j],a[j+1]);
}
else if(d[j]==d[j+1])
if(a[j]<a[j+1])
{
swap(d[j],d[j+1]);
swap(e[j],e[j+1]);
swap(a[j],a[j+1]);
}
else if(a[j]==a[j+1])
if(e[j]>e[j+1])
{
swap(d[j],d[j+1]);
swap(e[j],e[j+1]);
swap(a[j],a[j+1]);
}
}
}
for(i=1;i<=5;i++)
{
cout<<e[i]<<" "<<d[i]<<endl;
}
return 0;
}
by tgs9311 @ 2019-12-29 17:15:26
@XYC的Ship
#include<iostream>
#include<cstdio>
#include<stdlib.h>
using namespace std;
//用来练习自己实现二叉树排序
int N;
struct Tnode {
int id,chinese,math,english;
int LC,RC;
};
Tnode mytree[400];
int myfree=1;
void insert_node(int &root,int id,int chinese,int math,int english)
{
// cout<<root<<endl;
if(root==0)
{ //每次都是这里??
mytree[myfree].id=id;
mytree[myfree].chinese=chinese;
mytree[myfree].math=math;
mytree[myfree].english=english;
mytree[myfree].LC=mytree[myfree].RC=0;
root=myfree;
myfree++;
return;
}
// if(((chinese+math+english)>(mytree[root].chinese+mytree[root].math+mytree[root].english)) or //忘了比较总分
// (chinese>mytree[root].chinese) or (chinese==mytree[root].chinese and math>mytree[root].math) or
// (chinese==mytree[root].chinese and math==mytree[root].math and english>mytree[root].english) or
// (chinese==mytree[root].chinese and math==mytree[root].math and english==mytree[root].english and id<mytree[root].id))//注意,学号是比小
// {
// //更加“优秀”
// insert_node(mytree[root].RC,id,chinese,math,english);
// } 不能用or,要一个一个判断
if((chinese+math+english)>(mytree[root].chinese+mytree[root].math+mytree[root].english)) {
insert_node(mytree[root].RC,id,chinese,math,english);
return;
}
if(((chinese+math+english)==(mytree[root].chinese+mytree[root].math+mytree[root].english) )and (chinese>mytree[root].chinese)) { //else判断有问题,要加上相等,否则可能出现总分小语文达
insert_node(mytree[root].RC,id,chinese,math,english);
return;
}
if((chinese+math+english)==(mytree[root].chinese+mytree[root].math+mytree[root].english and chinese==mytree[root].chinese and id<mytree[root].id)) {
insert_node(mytree[root].RC,id,chinese,math,english);
return;
}
insert_node(mytree[root].LC,id,chinese,math,english);
}
int count=0;//只记录从右到左的5个
void print_tree(int root)
{
if(root==0) return;//还是0方便
print_tree(mytree[root].RC);//打印右边先
cout<<mytree[root].id<<" "<<(mytree[root].chinese+mytree[root].math+mytree[root].english)<<endl;
count++;
if(count>=5) exit(0);
print_tree(mytree[root].LC);//突然想起luck唱歌,好久以前
}
int main(void) {
cin>>N;
int root=0;//因该放在循环外面?是的,第一次过后root自动变为1
for(int i=0; i<N; i++)
{
int id,chinese,math,english;
cin>>chinese>>math>>english;
id=i;
insert_node(root,1+id,chinese,math,english);//由于是引用所以必须是变量不能是-1,而且每次都从根,但不是都是-1
}
// for(int i=0;i<=N;i++)
// {
// cout<<mytree[i].LC<<" "<<mytree[i].RC<<endl;
// }
print_tree(1);
return 0;
}
大难题,不懂你们怎么AC的