啊哈磊_编程从这里起步

标题: 12004 [打印本页]

作者: 推荐人    时间: 2015-10-12 12:32
标题: 12004
代码如下。
奖学金
[mw_shl_code=cpp,true]#include <iostream>
  
class student
{
    public:
    int number;
    int chinese;
    int math;
    int english;
    int total;
};
void _tswap(student &a,student &b)
{
    int t;
    t=a.number;a.number=b.number;b.number=t;
    t=a.chinese;a.chinese=b.chinese;b.chinese=t;
    t=a.math;a.math=b.math;b.math=t;
    t=a.english;a.english=b.english;b.english=t;
    t=a.total;a.total=b.total;b.total=t;
      
}
void _tsort(student *p,int count)
{
    int i,j;
    for(int i=0;i<count;i++)
    {
        for(int j=i+1;j<count;j++)
        {
            if( p.total<p[j].total)
            {
                _tswap(p,p[j]);
            }
            else if(p.total == p[j].total)
            {
                if(p.chinese < p[j].chinese)
                {
                    _tswap(p,p[j]);
                }
                else if(p.chinese == p[j].chinese)
                {
                    if(p.number>p[j].number)
                    {
                        _tswap(p,p[j]);
                    }
                }
            }
        }
    }
}
int main(void)
{
    const int zuigao = 5;
    int i;
    int count;
    std::cin >> count;
    student *p = new student[count];
    for(i=0;i<count;i++)
    {
        std::cin >> p.chinese >> p.math >> p.english;
        p.total = p.chinese + p.math + p.english;
        p.number = i+1;
    }
    _tsort(p,count);
    for(i=0;i<zuigao;i++)
    {
        std::cout << p.number << " " << p.total <<std::endl;
    }
    delete []p;
    p=NULL;
    return 0;
}
[/mw_shl_code]
作者: 推荐人    时间: 2015-10-12 13:09
自己写的代码
标识符有中文有英文请见谅。。。





欢迎光临 啊哈磊_编程从这里起步 (https://bbs.codeaha.com/) Powered by Discuz! X3.2