Help in a simple homework exercise
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...
精彩评论