標題:
排序 - array
[打印本頁]
作者:
tonyh
時間:
2022-12-15 20:35
標題:
排序 - array
本帖最後由 tonyh 於 2022-12-22 19:31 編輯
int n[]={5,7,3,9,8,1,2};
練習以各種可能的方式,針對 array 做遞增與遞減排序。
#include<bits/stdc++.h>
using namespace std;
int n[]= {5,7,3,9,8,1,2};
bool compare(int a, int b)
{
return a>b;
}
void show()
{
for(int i: n)
cout<<i<<" ";
cout<<endl;
}
int main()
{
int len=sizeof(n)/sizeof(n[0]);
show();
//遞增
sort(n, n+len);
//sort(begin(n),end(n));
show();
//遞減
//sort(rbegin(n),rend(n));
sort(n, n+len, compare);
//sort(n, n+len, greater<int>());
show();
return 0;
}
複製代碼
#include<bits/stdc++.h>
using namespace std;
int n[]= {5,7,3,9,8,1,2};
bool compare(int a, int b)
{
return a>b; //遞減
}
int main()
{
int len=sizeof(n)/sizeof(int);
//cout<<len<<endl;
for(int i: n)
cout<<i<<" ";
cout<<endl;
/*for(int i=0; i<len; i++)
cout<<n[i]<<" ";
cout<<endl;*/
cout<<"-----------------"<<endl;
//遞增
//sort(n,n+len);
sort(begin(n),end(n));
for(int i: n)
cout<<i<<" ";
cout<<endl;
cout<<"-----------------"<<endl;
//遞減
//sort(rbegin(n),rend(n));
//sort(begin(n),end(n),greater<int>()); //less<int>()
sort(begin(n),end(n),compare);
for(int i: n)
cout<<i<<" ";
cout<<endl;
return 0;
}
複製代碼
作者:
黃宥華
時間:
2022-12-22 19:35
#include<bits/stdc++.h>
using namespace std;
int n[]= {5,7,3,9,8,1,2,4,6};
int len=sizeof(n)/sizeof(int);
bool compare(int a, int b)
{
return a>b;
}
void show(){
cout<<"-----------------"<<endl;
for(int i: n)
cout<<i<<" ";
cout<<endl;
}
int main()
{
show();
sort(n,n+len);
//sort(begin(n),end(n));
show();
//sort(rbegin(n),rend(n));
//sort(begin(n),end(n),greater<int>()); //less<int>()
sort(n,n+len,compare);
show();
return 0;
}
複製代碼
作者:
李沛昂
時間:
2022-12-22 19:36
#include<bits/stdc++.h>
using namespace std;
int n[]= {5,7,3,9,8,1,2};
bool compare(int a, int b)
{
return a>b;
}
int main()
{
int len=sizeof(n)/sizeof(int);
for(int i: n)
cout<<i<<" ";
cout<<endl;
cout<<"-----------------"<<endl;
sort(begin(n),end(n));
for(int i: n)
cout<<i<<" ";
cout<<endl;
cout<<"-----------------"<<endl;
sort(begin(n),end(n),compare);
for(int i: n)
cout<<i<<" ";
cout<<endl;
return 0;
}
複製代碼
作者:
黃宇綸
時間:
2022-12-22 19:36
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int,int>
#define Graph vector<vector<int>>
#define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
const ll inf=(1ll<<63)-1;
const int maxn=1e5+5;
const ll mod=1e9+7;
int a[]={5,7,3,9,8,1,2};
int n=7;
bool so(int a,int b) { return a>b; }
signed main()
{
IOS();
sort(a,a+n);
//sort(begin(a),end(a));
REP(i,n) cout<<a[i]<<" "; cout<<"\n";
sort(a,a+n,so);
//sort(rbegin(a),rend(a));
REP(i,n) cout<<a[i]<<" ";cout<<"\n";
return 0;
}
複製代碼
作者:
黃宇瑄
時間:
2022-12-22 19:37
#include<bits/stdc++.h>
using namespace std;
int n[]= {5,7,3,9,8,1,2,4,6};
int len=sizeof(n)/sizeof(int);
bool compare(int a, int b)
{
return a>b;
}
void show()
{
for(int i: n)
cout<<i<<" ";
cout<<endl;
}
int main()
{
sort(n, n+len);
show();
sort(n, n+len, compare);
show();
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2