1 #include <bits/stdc++.h> 2 using namespace std; 3 inline int read() 4 { 5 int x=0,f=1; 6 char ch=getchar(); 7 while(ch<'0'||ch>'9') 8 { 9 if(ch=='-') 10 f=-1; 11 ch=getchar(); 12 } 13 while(ch>='0'&&ch<='9') 14 { 15 x=x*10+ch-'0'; 16 ch=getchar(); 17 } 18 return x*f; 19 } 20 inline void write(int x) 21 { 22 if(x<0) 23 { 24 putchar('-'); 25 x=-x; 26 } 27 if(x>9) 28 { 29 write(x/10); 30 } 31 putchar(x%10+'0'); 32 } 33 int a[100]; 34 int n; 35 int main() 36 { 37 cin>>n; 38 for(int i=1; i<=n; i++) 39 cin>>a[i]; 40 /* 42 for(int I =1; i<=n-1; i++) 43 { 44 for(int j=1; j<=n-i; j++) 45 { 46 if(a[j]>a[j+1]) 47 swap(a[j],a[j+1]); For (int I =1; int I =1; i<=n-1; i++) 53 { 54 for(int j=1; j<=n-i; j++) 55 { 56 if(a[j]<a[j+1]) 57 swap(a[j],a[j+1]); 58 } 59 } 60 for(int i=1; i<=n; i++) 61 cout<<a[i]<<" "; 62 return 0; 63}Copy the code

Case analysis:

1 / / question: enter an integer n, said there are n people, taking n lines each line corresponds to a name and the person's result, according to the output results from big to small order 2 / names/analysis: 3 #include <bits/stdc++. H > 4 using namespace STD; 5 inline int read() 6 { 7 int x=0,f=1; 8 char ch=getchar(); 9 while(ch<'0'||ch>'9') 10 { 11 if(ch=='-') 12 f=-1; 13 ch=getchar(); 14 } 15 while(ch>='0'&&ch<='9') 16 { 17 x=x*10+ch-'0'; 18 ch=getchar(); 19 } 20 return x*f; 21 } 22 inline void write(int x) 23 { 24 if(x<0) 25 { 26 putchar('-'); 27 x=-x; 28 } 29 if(x>9) 30 { 31 write(x/10); 32 } 33 putchar(x%10+'0'); 34 } 35 struct Student 36 { 37 char score; 38 char name[20]; 39 }p[100]; 40 int main() 41 { 42 int n; 43 cin>>n; 44 for(int i=1; i<=n; i++) 45 cin>>p[i].name>>p[i].score; 46 for(int i=1; i<=n-1; i++) 47 { 48 for(int j=1; j<=n-i; j++) 49 { 50 if(p[j].score<p[j+1].score) 51 swap(p[j],p[j+1]); 52 } 53 } 54 for(int i=1; i<=n; i++) 55 cout<<p[i].name<<endl; 56 return 0; 57 } 58 /* 59 5 60 huhu 5 61 haha 3 62 xixi 5 63 hengheng 2 64 gaoshou 8 65 */Copy the code

Time is O(n^2).