題目大意:node
按照題意將數用二叉樹的形式表示出來。將二叉樹按照要求形式輸出。ios
解題思路:spa
因爲有圖示,能夠輕易看出這個順序跟卡特蘭數列有關。且對於任意一個樹的任意子樹來講,右子樹至關於分針,左子樹至關於秒針。也就是說當右子樹要變換到下一種狀態時,左子樹要將它所能變換的狀態全變換完才能夠。code
下面是代碼:blog
#include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #define eps 1e-8 #define pi acos(-1.0) #define inf 107374182 #define inf64 1152921504606846976 #define lc l,m,tr<<1 #define rc m + 1,r,tr<<1|1 #define iabs(x) ((x) > 0 ? (x) : -(x)) #define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE)) #define clearall(A, X) memset(A, X, sizeof(A)) #define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE)) #define memcopyall(A, X) memcpy(A , X ,sizeof(X)) #define max( x, y ) ( ((x) > (y)) ? (x) : (y) ) #define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; long long Catalan[20]= {1,1,2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700,1767263190}; void does(long long num,int nodenum) { if(nodenum==1) { printf("X"); return ; } int p; long long temp=num; for(p=0; p<19; p++) { if(Catalan[p]*Catalan[nodenum-p-1]>=temp)break; temp-=Catalan[p]*Catalan[nodenum-p-1]; } if(p) { printf("("); does((temp-1)/Catalan[nodenum-p-1]+1,p); printf(")"); } printf("X"); if(nodenum-p-1) { printf("("); does((temp-1)%Catalan[nodenum-p-1]+1,nodenum-p-1); printf(")"); } } int main() { long long n,temp; int p; while(scanf("%lld",&n),n) { temp=n; for(p=1; p<19; p++) { if(Catalan[p]>=temp)break; temp-=Catalan[p]; } //printf("%d %d\n",p,temp); does(temp,p); puts(""); } return 0; }