返回列表 發帖

a225. 明明愛排列

本帖最後由 李知易 於 2025-2-8 12:08 編輯

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int a;
  6.     while(cin>>a)
  7.     {
  8.         int b[a];
  9.         int c[a];
  10.         for(int i=0;i<a;i++)
  11.         {
  12.             cin>>b[i];
  13.             c[i]=b[i]%10;
  14.         }
  15.         for(int i=0;i<a;i++)
  16.         {
  17.             for(int j=i+1;j<a;j++)
  18.             {
  19.                 if(c[i]>c[j])
  20.                 {
  21.                     swap(c[i],c[j]);
  22.                     swap(b[i],b[j]);
  23.                 }
  24.                 if(c[i]==c[j])
  25.                 {
  26.                     if(b[i]<b[j])
  27.                     {
  28.                         swap(c[i],c[j]);
  29.                         swap(b[i],b[j]);
  30.                     }
  31.                 }
  32.             }
  33.         }
  34.         for(int i=0;i<a;i++)
  35.         {
  36.             cout<<b[i]<<" ";
  37.         }
  38.         cout<<endl;
  39.     }
  40.     return 0;
  41. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int n;
  6.     while(cin>>n){
  7.         string s[n];
  8.         string s1[n];

  9.         for(int i=0;i<n;i++){
  10.             cin>>s[i];
  11.             string a=s[i];
  12.             s1[i]=a[a.length()-1];
  13.         }
  14.         for(int i=0;i<n-1;i++)
  15.         {
  16.             for(int j=i+1;j<n;j++){
  17.                 if(s1[i]>s1[j]){
  18.                     swap(s1[i],s1[j]);
  19.                     swap(s[i],s[j]);
  20.                 }else if(s1[i]==s1[j]){
  21.                     int x=stoi(s[i]);
  22.                     int y=stoi(s[j]);
  23.                     if(x<y){
  24.                         swap(s[i],s[j]);
  25.                     }
  26.                 }

  27.             }

  28.         }for(int i=0;i<n;i++){
  29.             cout<<s[i]<<" ";
  30.         }
  31.         cout<<endl;
  32.     }
  33.     return 0;
  34. }
複製代碼

TOP

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. int main()
  5. {
  6.     while(cin>>n)
  7.     {
  8.         int sum[n],cnt[n];
  9.         for(int i=0; i<n; i++)
  10.         {
  11.             cin>>sum[i];
  12.         }
  13.         for(int i=0; i<n; i++)
  14.         {
  15.             cnt[i]=sum[i]%10;
  16.         }
  17.         for(int i=0; i<n-1; i++)
  18.         {
  19.             for(int j=i+1; j<n; j++)
  20.             {
  21.                 if(cnt[j]<cnt[i])
  22.                 {
  23.                     swap(cnt[j],cnt[i]);
  24.                     swap(sum[j],sum[i]);
  25.                 }
  26.                 if(cnt[j]==cnt[i])
  27.                 {
  28.                     if(sum[j]>sum[i])
  29.                         swap(sum[j],sum[i]);
  30.                 }
  31.             }
  32.         }
  33.         cout<<sum[0];
  34.         for(int i=1; i<n; i++)
  35.         {
  36.             cout<<" "<<sum[i];
  37.         }
  38.         cout<<endl;
  39.     }
  40.     return 0;
  41. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int a;
  6.     while(cin>>a)
  7.     {
  8.         int b[a];
  9.         int c[a];
  10.         for(int i=0;i<a;i++)
  11.         {
  12.             cin>>b[i];
  13.         }
  14.         for(int i=0;i<a;i++)
  15.         {
  16.             c[i]=b[i]%10;
  17.         }
  18.         for(int i=0;i<a-1;i++)
  19.         {
  20.             for(int j=i+1;j<a;j++)
  21.             {
  22.                 if(c[j]<c[i])
  23.                 {
  24.                     swap(b[j],b[i]);
  25.                     swap(c[j],c[i]);
  26.                 }
  27.                 if(c[j]==c[i])
  28.                 {
  29.                     if(b[j]>b[i])
  30.                     {
  31.                         swap(b[j],b[i]);
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.         for(int i=0;i<a;i++)
  37.         {
  38.             cout<<b[i]<<" ";
  39.         }
  40.         cout<<endl;
  41.     }
  42.     return 0;
  43. }
複製代碼

TOP

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int t,s,tem;
  4. int main()
  5. {
  6.     while(cin>>s)
  7.     {
  8.         int n[s],f[s];
  9.         for(int i=0;i<s;i++)
  10.         {
  11.             cin>>t;
  12.             n[i]=t;
  13.             f[i]=t%10;
  14.         }
  15.         for(int i=0;i<s;i++)
  16.         {
  17.             for(int j=i+1;j<s;j++)
  18.             {
  19.                 if(f[j]<f[i])
  20.                 {
  21.                     swap(f[i],f[j]);
  22.                     swap(n[i],n[j]);
  23.                 }
  24.                 if(f[j]==f[i])
  25.                 {
  26.                     if(n[i]<n[j])
  27.                     {
  28.                         swap(n[i],n[j]);
  29.                     }
  30.                 }
  31.             }
  32.         }
  33.         for(int i=0;i<s;i++)
  34.         {
  35.             cout<<n[i]<<" ";
  36.         }
  37.         cout<<'\n';
  38.     }
  39. }
複製代碼
    ⪔〠   

TOP

返回列表