標題:
a225. 明明愛排列
[打印本頁]
作者:
李知易
時間:
2025-2-8 00:59
標題:
a225. 明明愛排列
本帖最後由 李知易 於 2025-2-8 12:08 編輯
[attach]20650[/attach]
作者:
洪承廷
時間:
2025-2-8 11:58
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
int b[a];
int c[a];
for(int i=0;i<a;i++)
{
cin>>b[i];
c[i]=b[i]%10;
}
for(int i=0;i<a;i++)
{
for(int j=i+1;j<a;j++)
{
if(c[i]>c[j])
{
swap(c[i],c[j]);
swap(b[i],b[j]);
}
if(c[i]==c[j])
{
if(b[i]<b[j])
{
swap(c[i],c[j]);
swap(b[i],b[j]);
}
}
}
}
for(int i=0;i<a;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
return 0;
}
複製代碼
作者:
高鋐鈞
時間:
2025-2-8 12:03
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(cin>>n){
string s[n];
string s1[n];
for(int i=0;i<n;i++){
cin>>s[i];
string a=s[i];
s1[i]=a[a.length()-1];
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++){
if(s1[i]>s1[j]){
swap(s1[i],s1[j]);
swap(s[i],s[j]);
}else if(s1[i]==s1[j]){
int x=stoi(s[i]);
int y=stoi(s[j]);
if(x<y){
swap(s[i],s[j]);
}
}
}
}for(int i=0;i<n;i++){
cout<<s[i]<<" ";
}
cout<<endl;
}
return 0;
}
複製代碼
作者:
楊惇翔
時間:
2025-2-8 12:05
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(cin>>n)
{
int sum[n],cnt[n];
for(int i=0; i<n; i++)
{
cin>>sum[i];
}
for(int i=0; i<n; i++)
{
cnt[i]=sum[i]%10;
}
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(cnt[j]<cnt[i])
{
swap(cnt[j],cnt[i]);
swap(sum[j],sum[i]);
}
if(cnt[j]==cnt[i])
{
if(sum[j]>sum[i])
swap(sum[j],sum[i]);
}
}
}
cout<<sum[0];
for(int i=1; i<n; i++)
{
cout<<" "<<sum[i];
}
cout<<endl;
}
return 0;
}
複製代碼
作者:
孫子傑
時間:
2025-2-8 12:05
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
int b[a];
int c[a];
for(int i=0;i<a;i++)
{
cin>>b[i];
}
for(int i=0;i<a;i++)
{
c[i]=b[i]%10;
}
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(c[j]<c[i])
{
swap(b[j],b[i]);
swap(c[j],c[i]);
}
if(c[j]==c[i])
{
if(b[j]>b[i])
{
swap(b[j],b[i]);
}
}
}
}
for(int i=0;i<a;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
return 0;
}
複製代碼
作者:
徐啟祐
時間:
2025-2-8 12:05
#include<bits/stdc++.h>
using namespace std;
int t,s,tem;
int main()
{
while(cin>>s)
{
int n[s],f[s];
for(int i=0;i<s;i++)
{
cin>>t;
n[i]=t;
f[i]=t%10;
}
for(int i=0;i<s;i++)
{
for(int j=i+1;j<s;j++)
{
if(f[j]<f[i])
{
swap(f[i],f[j]);
swap(n[i],n[j]);
}
if(f[j]==f[i])
{
if(n[i]<n[j])
{
swap(n[i],n[j]);
}
}
}
}
for(int i=0;i<s;i++)
{
cout<<n[i]<<" ";
}
cout<<'\n';
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2