ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1149. Sinus Dances

Nice recursive functions
Posted by † Ленин † [Yaroslavl SU] 20 Mar 2010 18:21
I thought i'll never get AC at this problem, but after some time i wrote that very nice (as for me ^___^) functions doing the work.

void a( int n, int k )
{
-printf("sin(%d",k);
-if ( k < n )
-{
--if ( k % 2 )
---printf("-");
--else
---printf("+");
--a( n, k + 1 );
-}
-printf(")");
}

void s( int n, int k )
{
-if ( k < n )
-{
--printf("(");
--s( n, k + 1 );
--printf(")");
-}
-a( n - k + 1, 1 );
-printf("+%d",k);
}

For answer just need to call s( n, 1 ).
Good luck!

Edited by author 20.03.2010 18:23
Re: Nice recursive functions
Posted by pusho4eg 25 Apr 2011 00:48
void s(int n, int k)
{ printf("sin(%d",k);
  if (n!=k)
  { putchar(((k&1)<<1)+43);
    s(n,++k);
  }
  putchar(')');
}

void p(int n, int k)
{ if (k>1)
  {  putchar('(');
     p(n,k-1);
  }
  s(k,1);
  printf("+%d",n+1-k);
  if (k<n) putchar(')');
}

my algorithm... for answer p(n,n)
Re: Nice recursive functions
Posted by Shah Habibul Imran 11 Oct 2018 12:30
Thanks, I was stuck in the recursion for Sn. :)