3 条题解

  • 1
    @ 2024-7-30 22:54:52

    pair ,一个优雅的函数(bushi

    #include<bits/stdc++.h>
    using namespace std;
    bool cmp(pair<int,pair<int,pair<int,string>>> necho1,pair<int,pair<int,pair<int,string>>> necho2) {
        if (necho1.first!=necho2.first)
            return necho1.first<necho2.first;
        if (necho1.second.first!=necho2.second.first)
            return necho1.second.first<necho2.second.first;
        if (necho1.second.second.first!=necho2.second.second.first)
            return necho1.second.second.first<necho2.second.second.first;
        return false;
    }
    int main() {
        int n=0;
        cin>>n;
        vector<pair<int,pair<int,pair<int,string>>>> ovo(n+1);
        for(int i=0;i<n;i++){
            cin>>ovo[i].second.second.second>>ovo[i].first>>ovo[i].second.first>>ovo[i].second.second.first;
        }
        sort(ovo.begin(),ovo.begin()+n,cmp);
        for(int i=0;i<n;i++){
            cout<<ovo[i].second.second.second<<endl;
        }
        return 0;
    }
    

    注意,sort的快排有不稳定性,有点问题(最后一个点过不了)还是得手写cmp

    因为感觉比楼上优雅所以发了,还有,请使用 vector 以够度过一个相对成功的码生(bushi

    • 1
      @ 2024-3-10 16:05:54

      答案

      #include<bits/stdc++.h>
      using namespace std;
      typedef struct
      {
          string name;
          int y;
          int m;
          int d;
          int id;
      }ss;
      ss sr[200];
      bool pd(ss a,ss b){
          if (a.y < b.y){
              return true;
          }
          else if(a.y == b.y){
              if (a.m < b.m){
                  return true;
              }
              else if (a.m == b.m){
                  if (a.d < b.d){
                      return true;
                  }
                  else if (a.d == b.d){
                      if (a.id > b.id){
                          return true;
                      }
                      else{
                          return false;
                      }
                  }
                  else{
                      return false;
                  }
              }
              else{
                  return false;
              }
          }
          else{
              return false;
          }
      }
      int main(){
          int n;
          cin >>n;
          for(int i = 0;i<n;i++){
              cin >>sr[i].name>>sr[i].y>>sr[i].m>>sr[i].d;
              sr[i].id = i;
          }
          sort(sr,sr + n,pd);
          for (int i = 0;i<n;i++){
              cout <<sr[i].name<<endl;
          }
      }
      
      • 0
        @ 2024-3-10 16:05:51
        using namespace std;
        struct zb
        {
        	string name;
        	int nian;
        	int yue;
        	int ri;
        	int id;
        }g[101];
        bool paixu(zb a, zb b)
        {
        	if (a.nian < b.nian)return 1;
        	else if (a.nian == b.nian) {
        		if (a.yue < b.yue)return 1;
        		else if (a.yue==b.yue){
        			if (a.ri < b.ri)return 1;
        			else if (a.ri == b.ri) {
        				if (a.id > b.id)return 1;
        			}
        		}
        	}
        	return 0;
        }
        int  main() {
        	int n;
        	cin >> n;
        	for (int i = 1; i <= n; i++) {
        		cin >> g[i].name >> g[i].nian >> g[i].yue >> g[i].ri;
        		g[i].id = i;
        	}
        	sort(g + 1, g + 1 + n, paixu);
        	for (int i = 1; i <= n; i++) {
        		cout << g[i].name<<endl;
        	}
        	return 0;
        }
        
        • 1

        信息

        ID
        718
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        (无)
        递交数
        105
        已通过
        21
        上传者