主题:一道关于数组排序的题,
haowonderful
[专家分:0] 发布于 2011-04-25 21:46:00
一道关于数组的题。输入n个学生的姓名及数学课成绩,将成绩降序排列,姓名顺序做相应调整,输出排列后的数组
回复列表 (共4个回复)
沙发
chb871013 [专家分:70] 发布于 2011-05-02 09:23:00
用结构体把分数和姓名装在一起,然后就这样用
struct elem
{
unsigned score;
char* name;
};
排序的时候就是
elem.score
然后交换顺序的时候就是
swap(elem* a,elem* b);
板凳
hesyond [专家分:0] 发布于 2011-05-07 23:24:00
初学者写的程序 应该写得不太简单 方法也不太好 但是可以实现你的功能
#include <stdio.h>
#define NULL 0
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
unsigned score;
long int num;
char name[20];
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%d,%ld,%s",&p1->score,&p1->num,p1->name);
getchar();
head=NULL;
while(p1->score!=0){
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%d,%ld,%s",&p1->score,&p1->num,p1->name);
getchar();
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
printf("The messages are:\n");
if(head!=NULL){
do{
printf("%d,%ld,%s\n",p->score,p->num,p->name);
p=p->next;
}while(p!=NULL);}
}
main(){
struct student *head;
struct student m,*p;
int i;
head=creat();
print(head);
p=head;
for(i=0;i<n-1;i++)
{
do{
if(p->score<p->next->score)
{
m.score=p->score;
p->score=p->next->score;
p->next->score=m.score;
m.num=p->num;
p->num=p->next->num;
p->next->num=m.num;
for(i=0;i<20;i++)
{
m.name[i]=p->name[i];
p->name[i]=p->next->name[i];
p->next->name[i]=m.name[i];
}
}
p=p->next;
}while(p->next!=NULL);
p=head;
}
printf("After the sort ");
print(head);
}
3 楼
cxxcomp [专家分:2370] 发布于 2011-05-08 13:17:00
练习STL的一个典型题目. 给一个C++版本的. 抛砖引玉.
--------------------------------------------------------
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class student
{
public:
friend ostream& operator<< (ostream& os, student& st)
{
os << "name: "
<< st._name
<< '\n'
<< "score: "
<< st._score << endl;
return os;
}
friend istream& operator>> (istream& is, student& st)
{
std::cout << "input name: ";
is >> st._name;
std::cout << "input score: ";
is >> st._score;
return is;
}
student()
: _name("none"), _score(0.0) {}
student(string name, float score)
: _name(name), _score(score) {}
student(const student& st)
{
_name = st._name;
_score = st._score;
}
~student() {}
bool operator<(const student& st)
{
return (this->_score < st._score);
}
student& operator=(const student& st)
{
this->_name = st._name;
this->_score = st._score;
return *this;
}
float GetScore() const
{
return _score;
}
protected:
private:
string _name;
float _score;
};
inline bool f_comp(const student& st1, const student& st2)
{
return st1.GetScore() < st2.GetScore();
}
template<class T>
inline void f_output(const T& begin, const T& end, const string& comment = ":")
{
std::cout << comment << "\n";
for (T it = begin; it != end; ++it)
{
std::cout << *it;
}
std::cout << endl;
}
int main(int, char **, char **)
{
const int number_st = 5;
vector<student> students(number_st);
for (int ix = 0; ix < number_st; ++ix)
{
std::cin >> students[ix];
}
f_output(students.begin(), students.end(), "unsorted: ");
sort(students.begin(), students.end(), f_comp);
f_output(students.begin(), students.end(), "sorted: ");
}
4 楼
cgl_lgs [专家分:21040] 发布于 2011-05-09 13:58:00
用qsort呗。
我来回复