ceil函数使用存疑
ZOJ 1049的解决代码(C++):
#include <stdio.h>
#include <math.h>
int main()
{
int num;
scanf("%d", &num);
for(int i = 1; i <= num; i++)
{
float x, y;
scanf("%f %f", &x, &y);
float area = M_PI_2 * (x * x + y * y);
int result = (int)ceil(area / 50.0);
printf("Property %d: This property will begin eroding in year %d.\n", i, result);
}
printf("END OF OUTPUT.\n");
return 0;
}
根据文档,ceil函数的参数类型为double、float、long double和Integral.
在解决这道题目时,如果将变量area和变量x、y同时设置为double类型时,result溢出。
- Code::Blocks 13.12
- compiler: GNU GCC Compiler