HDOJ 1236 with three methods using C++

来源:互联网 发布:视频加边框软件是什么 编辑:程序博客网 时间:2024/06/08 13:18

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
原创粉丝点击