开发者

Help in a simple homework exercise

开发者 https://www.devze.com 2022-12-19 03:07 出处:网络
I need to create a method, in the following signature: int x (int y); That\'s the example of values that this it should return:

I need to create a method, in the following signature:

int x (int y);

That's the example of values that this it should return:

x(3) = 1
x(4) = 1
x(5) = 2
x(6) = 2
x(7) = 3
x(8) = 3
x(9) = 4
x(10) = 4
...

Any ideas how could I do it?

Thank you.

EDIT: That's what I've got so far:

    static int x(int y)
    {
        return (y / 2) - 1;
开发者_如何转开发    }

but the problem is that:

x(3) = 0
x(4) = 1
x(5) = 1
x(6) = 2


Subtract 1 then integer divide by 2.


If you want to make a joke of those who asked you to do this (and if you know only values for 3 .. 10), you could also write the following method:

static int x(int y) {
  return (int)(10.0 * Math.Sin((double)y / 21.0));
}

It's probably not what they meant, but it should give the same results for arguments from 3 to 10 :-). And how did I find it? I know the graph of sin function, which is ascending in the beginning. Then I just tried to find out some 'magic constants' to find a configuration in which it returns the numbers you wanted...

0

精彩评论

暂无评论...
验证码 换一张
取 消