辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

1.10 编程基础之简单排序(10)

  1. 谁考了第k名,http://noi.openjudge.cn/ch0110/01/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    struct student {
        string id;
        float score;
    }s[100];
    
    bool comp(student a,student b){
        return a.score > b.score;
    }
    
    int main(){
        int n,k;
        
        cin >> n >> k;
        for(int i=0;i<n;i++){
            cin >> s[i].id >> s[i].score;
        }
        sort(s,s+n,comp);
        
        cout << s[k-1].id << " " << s[k-1].score << endl;
        
        return 0;
    }
    
  2. 奇数单增序列,http://noi.openjudge.cn/ch0110/02/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int a[500];
    bool comp(int a,int b){
      return a < b;	
    }
    
    int main(){
        int n,tmp,p=0;
        cin >> n;
        for(int i=0;i<n;i++){
            cin >> tmp;
            if(tmp%2==1)
                a[p++]=tmp;
        }
        sort(a,a+p,comp);
        
        cout << a[0];
        for(int i=1;i<p;i++)
            cout << "," << a[i];
        cout << endl;
        
        return 0;
    }
    
  3. 成绩排序,http://noi.openjudge.cn/ch0110/03/
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    struct Score {
      string name;
      int score;
    };
    
    Score s[100];
    int n;
    
    bool comp(Score a,Score b){
      if(a.score>b.score)
        return true;
      else if(a.score==b.score){
        if(a.name.compare(b.name)<0)
          return true;
      }
      return false;
    }
    
    int main(){
    
      cin >> n;
      for(int i=0;i<n;i++)
        cin >> s[i].name >> s[i].score;
    
      sort(s,s+n,comp);		
      
      for(int i=0;i<n;i++)
        cout << s[i].name << " " << s[i].score << endl;
        
      return 0;
    }
    
  4. 奖学金,http://noi.openjudge.cn/ch0110/04/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    struct Student{
      int no;
      int chinese;
      int math;
      int english;
      int total;
    };
    
    Student s[305];
    int n;
    
    bool comp(Student a,Student b){
      if(a.total>b.total)
        return true;
      else if(a.total == b.total)
        if(a.chinese>b.chinese)
          return true;
        else if(a.chinese == b.chinese)
          if(a.no<b.no)
            return true;		
      return false;
    }
    
    int main(){
      cin >> n;
      for(int i=1;i<=n;i++){
        s[i].no=i;
        cin >> s[i].chinese >> s[i].math >> s[i].english;
        s[i].total = s[i].chinese+s[i].math+s[i].english;
      }	
      
      sort(s+1,s+n+1,comp);
      
      for(int i=1;i<=5;i++){
        cout << s[i].no << " " << s[i].total << endl;
      }
      
      return 0;
    }
    
  5. 分数线划定,http://noi.openjudge.cn/ch0110/05/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int n,m,line;
    
    struct Candidate{
      int id;
      int score;
    };
    
    Candidate candi[5005];
    
    bool comp(Candidate a,Candidate b){
      if(a.score>b.score)
        return true;
      else if(a.score == b.score)
        if(a.id < b.id)
          return true;
      return false;
    }
    
    int main(){
      cin >> n >> m;
      for(int i=0;i<n;i++)
        cin >> candi[i].id >> candi[i].score;
      sort(candi,candi+n,comp);
      int p = m*1.5;
      int cnt = 1;
      for(int i=0;i<n;i++){
        if(cnt<p){
          cnt++;
        }else{
          line = candi[i].score;
          break;
        }
      }	
      cnt = 0;
      for(int i=0;i<n;i++){
        if(candi[i].score>=line){
          cnt++;
        }else{
          break;
        }
      }
      cout << line << " " << cnt << endl;
      for(int i=0;i<cnt;i++)
        cout << candi[i].id << " " << candi[i].score << endl;
      
      return 0;
    }
    
  6. 整数奇偶排序,http://noi.openjudge.cn/ch0110/06/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int n,a[20],b[20],al=0,bl=0;
    
    bool comp(int a,int b){
      return a>b;	
    }
    
    int main(){
      for(int i=0;i<10;i++){
        cin >> n;
        if(n%2==0){
          b[bl++]=n;
        } else{
          a[al++]=n;
        }
      }
      sort(a,a+al,comp);
      sort(b,b+bl);
      for(int i=0;i<al;i++)
        cout << a[i] << " ";
      for(int i=0;i<bl;i++)
        cout << b[i] << " ";
      cout << endl;
      
      return 0;
    } 
    
  7. 合影效果,http://noi.openjudge.cn/ch0110/07/
    #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    double f[40],m[40],h;
    int n,fl=0,ml=0;
    string name;
    
    bool comp(double x,double y){
      return x>y;
    }
    
    int main(){
      cin >> n;
      for(int i=0;i<n;i++){
        cin >> name >> h;
        if(name.compare("male")==0){
          m[ml++]=h;
        }
        if(name.compare("female")==0){
          f[fl++]=h;
        }
      }
      sort(m,m+ml);
      sort(f,f+fl,comp);
      for(int i=0;i<ml;i++)
        printf("%.2f ",m[i]);
      for(int i=0;i<fl;i++)
        printf("%.2f ",f[i]);
      cout << endl; 
      
      return 0;
    }
    
  8. 病人排队,http://noi.openjudge.cn/ch0110/08/
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    struct P{
      string no;
      int age;
    };
    
    P p1[100],p2[200];
    
    int n,age,l1=0,l2=0;
    string no;
    
    bool comp(P a,P b){
      return a.age > b.age;
    }
    
    int main(){
      cin >> n;
      for(int i=0;i<n;i++){
        cin >> no >> age;
        if(age>=60){
          p1[l1].no = no;
          p1[l1].age = age;
          l1++;
        }else{
          p2[l2].no = no;
          p2[l2].age = age;
          l2++;
        }
      }	
      stable_sort(p1,p1+l1,comp);
      for(int i=0;i<l1;i++)
        cout << p1[i].no << endl;
      for(int i=0;i<l2;i++)
        cout << p2[i].no << endl;
        
      return 0;
    }
    
  9. 明明的随机数,http://noi.openjudge.cn/ch0110/09/
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int n,num[105];
    
    int main(){
      cin >> n;
      for(int i=0;i<n;i++){
        cin >> num[i];
      }
      sort(num,num+n);
      int flag = num[0],cnt=1;
      for(int i=1;i<n;i++){
        if(num[i]!=flag){
          flag=num[i];
          cnt ++;
        }
      }
      cout << cnt << endl; 		
      flag = num[0];
      cout << flag;
      for(int i=1;i<n;i++){
        if(num[i]!=flag){
          flag=num[i];
          cout << " " << flag;
        }
      }
      cout << endl;
      
      return 0;
    }
    
  10. 单词排序,http://noi.openjudge.cn/ch0110/10/
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <set>
    #include <iterator>
    using namespace std;
    
    set<string> words;
    string s;
    
    int main(){
      while(cin>>s){
        words.insert(s);
      }
      for(set<string>::iterator p=words.begin();p!=words.end();p++){
        cout << *p << endl;
      }
      
      return 0;
    }
    

参考网址

  1. http://noi.openjudge.cn