It's the same problem yesterday, http://www.cnblogs.com/092-zhang/p/4148925.html.
Yesterday that program's time complexity in my ability to basically no longer optimize, the space complexity of O (2^n), resulting in with the increase in the size of the input increases.
It was suddenly found that this figure is a variant of the Yang Hui Triangle, through mathematical methods to optimize the algorithm.
1 /*unusual Triangle*/2#include <stdio.h>3#include <stdlib.h>4#include <time.h>5 6 intCOUNT2N (LongN//calculates the number of ' factor 2 ' in the factorial of n7 {8 intrlt=0;9 Ten while(n/2>0) One { An = n/2; -RLT + =N; - } the - returnRLT; - } - + intMainvoid) - { + LongN; A LongI, J; at Char*outputstr[2] = {" ","* "}; - intIsstar; - clock_t start, finish; - Doubleduration; - -Start =clock (); in while(SCANF ("%d", &n)! = EOF && N! =0) - { toprintf"The Triangle scale is %d:\n", n); + -n =1<< (n1);//Determine size the for(i=0; i<n; i++)//Cycle Printing * { $Isstar =count2n (i);Panax Notoginseng for(j=i;j<n;j++)//print the preceding spaces -printf (*outputstr+1); the + for(j=0; j<=i; J + +)//Print Flower Printing A if(Isstar-count2n (j)-count2n (I-J) <=0) theprintf (* (outputstr+1)); + Else -printf (*outputstr); $printf"\ n"); $ } - } - thefinish =clock (); -Duration = (Double) (Finish-start)/clocks_per_sec;Wuyiprintf"%f seconds\n", duration); the - returnexit_success; Wu}
There was no limit on the size of the input, and then the efficiency of the individual was satisfied.
Yang Hui triangular type----derivative