Yang Hui triangular type----derivative

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.