题解 | #取近似值#
取近似值
http://www.nowcoder.com/practice/3ab09737afb645cc82c35d56a5ce802a
读入浮点数f,强转为到整型以获取其整数部分d,f-d获取其小数部分,判断小数部分是否大于等于0.5,如果满足条件则需要返回d+1,否则返回d
#include <stdio.h> int fun(float f) { int d = int(f); if(f-d >= 0.5) return d+1; else return d; } int main() { float f; while(scanf("%f", &f) != EOF) { printf("%d\n",fun(f)); } return 0; }