Guest User

Untitled

a guest
Feb 13th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #define MAX_N 30
  4.  
  5. unsigned int count[MAX_N+1];
  6.  
  7. void recur(int a, int b, int n){
  8. if(b)++count[n];
  9. if(n<MAX_N){
  10. recur(a+1,b,n+1);
  11. if(b+1<a)recur(a,b+1,n+1);
  12. }
  13. }
  14. int main()
  15. {
  16. time_t t0,t1,t2;
  17. int a,b,i,n,p2,t,x;
  18.  
  19. t0=clock();
  20. for(n=1;n<=MAX_N;++n){
  21. p2=1<<n; // equivalent to ipow(2,n)
  22. for(i=t=0;i<p2;++i){
  23. a=b=0;
  24. for(x=p2/2;x;x/=2){
  25. if(i&x)++a;
  26. else ++b;
  27. if(b>=a)break;
  28. }
  29. if(a>b)++t;
  30. }
  31. printf("%2d %9d\n",n,t-1);
  32. }
  33. t1=clock();
  34. printf("%f\n\n",(double)(t1-t0)/CLOCKS_PER_SEC);
  35.  
  36. recur(1,0,1);
  37. for(i=1;i<=MAX_N;++i)
  38. printf("%2d %9u\n",i,count[i]);
  39. t2=clock();
  40. printf("%f\n",(double)(t2-t1)/CLOCKS_PER_SEC);
  41.  
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment