看到一个很好的题,写了半天,没调出来。大家一起研究看看。
大学英语4级考试,想知道全校第3名学生的成绩是多少?
如果最高分有好多个学生,则相同成绩的学生都算第一名;同理,如果第二高分的有多个学生,都算第二名。
当然这是简单题,请你快速编一个程序找到第3名的成绩。
输入:输入有多组,每组有2行,第一行是学生人数N(1<=N<10000),第二行有N个整数,分别表示每个学生的成绩(0到1e9)。当输入的N为0的时候结束程序。
输出:对于每组输入,输出只有一行,即第3名学生的成绩,如果找不到,则输出No such score !
Sample input:
10
90 84 90 60 70 65 73 85 98 98
5
90 90 89 90 90
0
Sample output:
85
No such score !
如果最高分有好多个学生,则相同成绩的学生都算第一名;同理,如果第二高分的有多个学生,都算第二名。
当然这是简单题,请你快速编一个程序找到第3名的成绩。
输入:输入有多组,每组有2行,第一行是学生人数N(1<=N<10000),第二行有N个整数,分别表示每个学生的成绩(0到1e9)。当输入的N为0的时候结束程序。
输出:对于每组输入,输出只有一行,即第3名学生的成绩,如果找不到,则输出No such score !
Sample input:
10
90 84 90 60 70 65 73 85 98 98
5
90 90 89 90 90
0
Sample output:
85
No such score !
我的想法是用二叉树,然后递归rank
个人感觉算法还 ok这里是我的code
但是貌似调不过
程序代码:#include<iostream>
using namespace std;
struct BiNode{
long key;
BiNode *pre;
BiNode *next;
BiNode(long n){ key =n ;pre=NULL; next=NULL;}
};
class BiTree{
private:
static int Rank;
void BiTreeInsert(BiNode *&root,long n)
{
if( NULL == root)
{
root= new BiNode(n);
}
else
{
if( root->key < n )
root->pre=new BiNode(n);
if(root->key >n )
root->next=new BiNode(n);
if( root->key == n)
return ;
}
}
void BiTreeRank(BiNode * root, int r , long & key)
{
if( NULL == root)
{
return ;
}
else
{
BiTreeRank( root->next, r , key);
Rank++;
if( Rank == r)
key=root->key;
BiTreeRank( root->pre, r, key);
}
}
void BiTreeShow(BiNode * root )
{
if(NULL == root)
return ;
BiTreeShow(root->pre);
cout<<root->key<<" ";
BiTreeShow(root->next);
}
void BiTreeDestroy(BiNode *root){
if( NULL == root)
return ;
BiTreeDestroy(root->pre);
BiTreeDestroy(root->next);
delete root;
}
public:
BiNode *root;
BiTree(){ root = NULL ;}
~BiTree()
{
// BiTreeDestroy(root);
}
void insert(long n){ BiTreeInsert(root, n);}
long rank(int r)
{
long k;
BiTreeRank(root,r, k);
return k;
}
void show(){ BiTreeShow( root);}
};
int BiTree::Rank=0;
int main()
{
int n;
long key;
while(cin>>n)
{
if ( 0 == n)
break;
BiTree *tree=new BiTree();
for( int i=0;i<n;i++)
{
cin>>key;
tree->insert(key);
}
tree->show();
cout<<endl;
int rank=-1;
rank=tree->rank(1);
if( -1 == rank )
cout<<"NO such score!"<<endl;
else
cout<<rank<<endl;
}
return 0;
}哪位高人帮check一下。









