d******r 发帖数: 4 | 1 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的? |
y***y 发帖数: 56 | 2 why not write by yourself? it's so simple.
【在 d******r 的大作中提到】 : 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
|
l*l 发帖数: 225 | 3 int(x+0.5)
【在 y***y 的大作中提到】 : why not write by yourself? it's so simple.
|
y***y 发帖数: 56 | 4 what if x is equal to interger.
【在 l*l 的大作中提到】 : int(x+0.5)
|
bz 发帖数: 1770 | 5
no, lib functions are all doing round off, but writing one is very
easy.
【在 d******r 的大作中提到】 : 我知道int是取整,有没有类似的取整函数但是算法是四舍五入的?
|
h**h 发帖数: 132 | 6 take it easy, think about so many non-cs majors changed to
Computer Science.
It is always hard to get started.
【在 l*l 的大作中提到】 : int(x+0.5)
|
y***y 发帖数: 56 | 7 He deleted his post because he know he made a mistake.
BUt it seems you still dont understand and self-feeling good.
What I mean is int(x+0.5) gives you x if x is an interger itself.
but 四舍五入 gives you x+1. so int(x+0.5) is a wrong answer. You need
to write if-else.
【在 h**h 的大作中提到】 : take it easy, think about so many non-cs majors changed to : Computer Science. : It is always hard to get started.
|
y***y 发帖数: 56 | 8 faint...it seems I made the mistake....anyway,sometimes brain
stalk....:(
【在 y***y 的大作中提到】 : He deleted his post because he know he made a mistake. : BUt it seems you still dont understand and self-feeling good. : What I mean is int(x+0.5) gives you x if x is an interger itself. : but 四舍五入 gives you x+1. so int(x+0.5) is a wrong answer. You need : to write if-else.
|
y***y 发帖数: 56 | 9 okay, I made a mistake, but the int(x+0.5) is still a wrong answer.
hohohoho~
【在 y***y 的大作中提到】 : faint...it seems I made the mistake....anyway,sometimes brain : stalk....:(
|
y***y 发帖数: 56 | 10 try some negative digits.
you still need to write if-else.
【在 y***y 的大作中提到】 : okay, I made a mistake, but the int(x+0.5) is still a wrong answer. : hohohoho~
|
|
|
y***y 发帖数: 56 | 11 正解:
#!/usr/local/perl
$x=;
if($x>=0){
print int($x+0.5);
}
else{
print int($x-0.5);}
win!! hohohoho~
【在 y***y 的大作中提到】 : try some negative digits. : you still need to write if-else.
|
p**h 发帖数: 99 | 12 how about this:
int i;
double d;
/* d = whatever number. */
i = (int)floor(d + 0.5);
【在 y***y 的大作中提到】 : 正解: : #!/usr/local/perl : $x=; : if($x>=0){ : print int($x+0.5); : } : else{ : print int($x-0.5);} : win!! hohohoho~
|
c*****t 发帖数: 1879 | 13 Sigh, you are the confused one :)
For the negative numbers, there are two rounding schemes.
【在 y***y 的大作中提到】 : 正解: : #!/usr/local/perl : $x=; : if($x>=0){ : print int($x+0.5); : } : else{ : print int($x-0.5);} : win!! hohohoho~
|
m*****e 发帖数: 4193 | 14 Mod maths treats it the same no matter you are + or -. Always the largest
int that is no larger than the number. This is consistent.
【在 c*****t 的大作中提到】 : Sigh, you are the confused one :) : For the negative numbers, there are two rounding schemes.
|
l*l 发帖数: 225 | 15 you are smart, but the code is too long, even it is sample.
I write one line and solve it, I guess it is the best code any one can
get.
print int(($x+abs($x))/2+0.5)+int(($x-abs($x))/2-0.5);
【在 y***y 的大作中提到】 : try some negative digits. : you still need to write if-else.
|
b******d 发帖数: 28 | 16 Best? How do you evaluate if a program is good or poor?
Do you think such code is in good style?
【在 l*l 的大作中提到】 : you are smart, but the code is too long, even it is sample. : I write one line and solve it, I guess it is the best code any one can : get. : print int(($x+abs($x))/2+0.5)+int(($x-abs($x))/2-0.5);
|