HDOJ 1236 with three methods using C++

HDOJ 1236 with three methods using C++

 

Conventional Method:
 
#include <iostream>
#include<algorithm>
#include <stdio.h>
using namespace std;
 
const int Max = 1010;
struct Student
{
       charname[30];
       intscore;
}s[Max];
 
int score[10];
 
bool cmp(const Student&a, const Student &b)
{
       if(a.score > b.score) return true;
       else if(a.score == b.score && strcmp(a.name, b.name)< 0) return true;
       else return false;
}
 
int main()
{
       intn, m, line, ps, pn, pass;
      
       while(scanf("%d",&n) && n)
       {
              scanf("%d%d",&m, &line);
              for(int i(0); i<m; ++i)     scanf("%d", score+i);
              for(inti(0); i<n; ++i)
              {
                     s[i].score= 0;
                     scanf("%s",s[i].name);
                    
                     scanf("%d",&ps);
                     for(intj(0); j<ps; ++j)
                     {
                            scanf("%d",&pn);
                            s[i].score+= score[pn-1];
                     }
              }
             
              sort(s, s+n, cmp);
              for(pass=0;pass < n && s[pass].score >= line; ++pass);
 
              printf("%d\n",pass);
              for(inti(0); i<pass; ++i) printf("%s%d\n", s[i].name, s[i].score);
       }
       system("pause");
       return0;
}
 
Operator Overloading Method:
 
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
 
const int Max = 1010;
struct Student
{
       charname[30];
       intscore;
 
       friend bool operator <(const Student &a, const Student&b)
       {
              if(a.score > b.score) return true;
              else if(a.score == b.score && strcmp(a.name,b.name) < 0) return true;
              else return false;
       }
};
 
Student s[Max];
int score[10];
 
int main()
{
       intn, m, line, ps, pn, pass;
      
       while(scanf("%d",&n) && n)
       {
              scanf("%d%d",&m, &line);
              for(inti(0); i<m; ++i)     scanf("%d",score+i);
              for(inti(0); i<n; ++i)
              {
                     s[i].score= 0;
                     scanf("%s",s[i].name);
                    
                     scanf("%d",&ps);
                     for(intj(0); j<ps; ++j)
                     {
                            scanf("%d",&pn);
                            s[i].score+= score[pn-1];
                     }
              }
             
              sort(s, s+n);
              for(pass=0;pass < n && s[pass].score >= line; ++pass);
 
              printf("%d\n",pass);
              for(inti(0); i<pass; ++i) printf("%s%d\n", s[i].name, s[i].score);
       }
       system("pause");
       return0;
}
 
Function Object Method:
 
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
 
const int Max = 1010;
struct Student
{
       charname[30];
       intscore;
};
 
Student s[Max];
int score[10];
 
class Comp
{
public:
       bool operator()(const Student &a, const Student &b)
       {
              if(a.score > b.score) return true;
              else if(a.score == b.score && strcmp(a.name,b.name) < 0) return true;
              else return false;
       }
};
 
int main()
{
       intn, m, line, ps, pn, pass;
      
       while(scanf("%d",&n) && n)
       {
              scanf("%d%d",&m, &line);
              for(inti(0); i<m; ++i)     scanf("%d",score+i);
              for(inti(0); i<n; ++i)
              {
                     s[i].score= 0;
                     scanf("%s",s[i].name);
                    
                     scanf("%d",&ps);
                     for(intj(0); j<ps; ++j)
                     {
                            scanf("%d",&pn);
                            s[i].score+= score[pn-1];
                     }
              }
             
              sort(s, s+n, Comp());
              for(pass=0;pass < n && s[pass].score >= line; ++pass);
 
              printf("%d\n",pass);
              for(inti(0); i<pass; ++i) printf("%s%d\n", s[i].name, s[i].score);
       }
       system("pause");
       return0;
}
 
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值