在C语言的库函数中,floor函数的语法如下:
#include <math.h>
double floor( double arg );
功能: 函数返回参数不大于arg的最大整数。如果是负数的话往绝对值小的取,例如,
x = 6.04;
y = floor( x );
y的值为6.0.
与floor函数对应的是ceil函数,即上取整函数。
有趣的是,floor在英文中是地板的意思,而ceil是天花板的意思,很形象地描述了下取整和上取整的数学运算。
方法floor表示
public static double floor(double a)
返回最大的(最接近正无穷大) double 值,该值小于等于参数,并等于某个整数。特殊情况如下:
- 如果参数值已经等于某个整数,那么结果与该参数相同。
- 如果参数为 NaN、无穷大、正 0 或负 0,那么结果与参数相同。
n<=S(k)=1/2k(k+1)==>k^2+k-2n>=0;==>[k-(-1+根号(1+8*n))/2]*[k-(-1-根号(1+8*n))/2)]>=0;
可直接求出k=[-1+根号(1+8*n)]/2;为了避免浮点误差,下面用了点小技巧
#include <stdio.h>
#include <math.h>
int main()
{
int n;
while(scanf("%d",&n)==1)
{
int k=(int)floor((sqrt(8.0*n+1)-1)/2-le-9)+1;
int s=k*(k+1)/2;
printf("%d/%d\n",k-s+n,s-n+1);
}
return 0;
}