题目:PAT1064
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int cmp(int a,int b) { return a < b; } int n; int cnt = 0; void CreateTree(int k,const int a[],int tree[]) { if(k >= n) return; CreateTree(2*k+1,a,tree); tree[k] = a[cnt++]; CreateTree(2*k+2,a,tree); } int main() { scanf("%d",&n); int a[n],tree[n]; for(int i=0;i<n;++i) scanf("%d",&a[i]); sort(a,a+n,cmp); CreateTree(0,a,tree); for(int i=0;i<n;++i) { if(i) printf(" "); printf("%d",tree[i]); } return 0; }