3591 - b3
请编写程序,它的功能是输入一个数n(0<n<=100),随后输入n个数,然后对这n个数按从小到大的顺序排序输出。
Input
输入有多组样例。
首先输入n ,接下来输入n个整数。
Output
输出排序后的n个数。 行末没有空格。
Examples
Input
5 45 67 6 8 78
Output
6 8 45 67 78
Solution C
#include <stdio.h> #include <stdlib.h> void bubble(int *a,int n) { int i,j,temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } int main() { int n; int x; int arr[100]; int i; while(scanf("%d",&n) !=EOF) { for( i=0; i <n;i++) { scanf("%d",&x); arr[i] =x; } bubble(arr,n); for(i=0;i<n-1;i++) { printf("%d ",arr[i]); } printf("%d\n",arr[i]); } return 0; }
Solution C++
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; bool cmp(int a,int b) { return a<b; } int main() { int n; while(cin>>n) { int a[105]; int i; for(i=0;i<n;i++) { cin>>a[i]; } sort(a,a+n,cmp); cout<<a[0]; for(i=01;i<n;i++) cout<<' '<<a[i]; cout<<endl; } return 0; }