Guys, I forgot my math. Faced a trivial task - it is necessary to express the dependence of y on x - f (x) = y through the formula, according to this data table:

x 1 2 3 4 5 6 7 8 9 10

y 1 1 2 2 3 3 4 4 5 5

  • one
    either round (x / 2) or ceil (x / 2). anything can be. - Yura Ivanov
  • one
    Method of least squares to help - Divon

2 answers 2

So?

double f(const double x) { return floor((x-1)/2+1); } 

    Of course, so:

     int f(int x) { static const table[] = { 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }; if (x < 1 || x > 10) throw std::invalid_argument("x"); return table[x]; } 

    Your K. O.


    You can, of course, int f(int x) { return (x + 1)/2; } int f(int x) { return (x + 1)/2; } , but this does not meet the requirements, since the behavior at the other points is not specified.