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

Simple Solution
Posted by Shaft 19 Aug 2015 19:40
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

string S, A;
unsigned short An = 1;
bool negative     = true;

void get_next_A();
void get_S(unsigned short);

int main()
{
    unsigned short input_n;

    cin >> input_n;

    get_S(input_n);

    cout << S << endl;

    return 0;
}

void get_next_A()
{
    if(An == 1)
        A = "sin(1)";
    else
    {
        A.erase(A.size() - An + 1, An);

        if(negative)
            A += "-sin(";
        else
            A += "+sin(";

        char buffer[5];

        itoa(An, buffer, 10);
        A += buffer;

        for(unsigned short j = 0; j < An; ++j)
            A += ")";

        negative = !negative;

    }

    ++An;
    return;
}

void get_S(unsigned short n)
{
    char _buffer[10];

    for(unsigned short i = 0; i < n - 1; ++i)
        S += '(';

    for(unsigned short i = n; i > 0; --i)
    {
        get_next_A();

        itoa(i, _buffer, 10);

        S += A;
        S += '+';
        S += _buffer;
        if(i != 1)
            S += ')';

    }

    return;
}
Re: Simple Solution
Posted by Egor 22 Oct 2016 16:42
You can make it simpler :)
For example, here is the function generating sine expressions:
string get_sine_expression(int x, int n)
{
  if (x == n) return "sin(" + to_string(x) + ")";

  char sign = x & 1 ? '-' : '+';

  return "sin(" + to_string(x) + sign + get_sine_expression(x + 1, n) + ")";
}