|
楼主 |
发表于 2022-7-19 12:32:24
|
显示全部楼层
冒泡:
- #include<iostream>
- #include<cstdio>
- using namespace std;
- const int MAXN=100;
- int n;
- int a[MAXN+1];
- int num;
- int ans[MAXN+1];
- void bubble_sort(int a[],int begin,int end){
- bool flag=true;
- while(flag){
- flag=false;
- for(int i=begin;i<end-1;++i){
- if(a[i]>a[i+1]){
- flag=true;
- swap(a[i],a[i+1]);
- }
- }
- }
- }
- int main(){
- cin>>n;
- for(int i=1;i<=n;i++){
- cin>>a[i];
- }
-
- bubble_sort(a,1,n+1);
-
- ++num;
- ans[num]=a[1];
- for(int i=2;i<=n;++i){
- if(a[i]==ans[num]){
- continue;
- }
- ++num;
- ans[num]=a[i];
- }
- cout<<num<<'\n';
- for(int i=1;i<=num;++i){
- cout<<ans[i]<<' ';
- }
- cout<<'\n';
- return 0;
- }
复制代码 |
|