很久没有到论坛上来了,也没有发布文章,最近在做个软件系统,没有什么时间,今天到了论坛上看到了第24次编程比赛之第1题的帖子,虽然已经结贴了,但是我很唱时间没有写dos界面的程序了,就写了一个也作为一篇文章发了,呵呵;题目是:
http://www.programfan.com/club/showbbs.asp?id=157283
//header1.h
#ifndef HEADER_Header1
#define HEADER_Header1
const int MaxNum=30;
class Match
{
private:
int scoreNode[MaxNum];
int Number[MaxNum];
int num;
public:
Match(){
for(int i=0;i<7;i++)
{
scoreNode[i]=0;
Number[i]=0;
}
num=0;
}
void SortScore();
void ScorePlace(int place[], const int score[], int numValue);
};
#endif
//Source1.cpp
#include <iostream>
#include "Header1.h"
#include <assert.h>
using namespace std;
extern const int MaxNum;
void Match::SortScore()
{
if(!scoreNode){
cout<<"No athelete score exist!"<<endl;
return;
}
int k,j,temptNode;
k=j=temptNode=0;
for(int i=0;i<num-1;i++)
{
k=i;
for(j=i+1;j<num;j++)
{
if(scoreNode[k]>scoreNode[j])
k=j;
}
if(k!=i)
{
temptNode=scoreNode[i];
scoreNode[i]=scoreNode[k];
scoreNode[k]=temptNode;
}
}
}
void Match::ScorePlace(int place[], const int score[], int numValue)
{
assert(scoreNode);
num=numValue;
int i=0;
while(i<numValue)
{
scoreNode[i]=score[i];
i++;
}
SortScore();
int j=0;
bool flag;
for(int i=0;i<numValue;i++)
{
flag=false;
if(score[j]==scoreNode[i])
{
flag=true;
place[j]=i+1;
}
if(flag)
{
if(j<numValue-1)
{
i=-1;
j++;
}
else
break;
}
}
for(int i=0;i<numValue;i++)
scoreNode[i]=place[i];
SortScore();
int k,tempt,m;
for(int i=0;i<numValue;i++)
{
tempt=scoreNode[i];
k=i+1;
if(scoreNode[k]==tempt)
{
while(scoreNode[k]==tempt&&k<numValue)
k++;
for(m=k;m<numValue;m++)
scoreNode[m]-=1;
}
}
cout<<"The final athelete numbers are:"<<endl;
int temptnum;
for(int j=0;j<numValue;j++)
{
temptnum=place[j]-1;
cout<<" "<<scoreNode[temptnum];
}
}
//Main函数
#include <iostream>
#include "Source1.cpp"
using namespace std;
extern const int MaxNum;
int score[MaxNum];
int _tmain(int argc, _TCHAR* argv[])
{
int number;
cout<<endl<<"please input the number of the atheletes:";
cin>>number;
while(number>MaxNum||number<=0){
cout<<"error happen!Either too many atheletes or the num is minus!"<<endl;
cout<<"Correct it and input the number:";
}
cout<<"please input all atheletes' score:";
for(int i=0;i<number;i++)
cin>>score[i];
int* place=new int[number];
Match* CreateMath=new Match();
CreateMath->ScorePlace(place,score,number);
return 0;
}
评论