链接
http://ac.jobdu.com/problem.php?pid=1062
-
题目描述:
-
编写程序,计算下列分段函数y=f(x)的值。
y=-x+2.5; 0<=x<2
y=2-1.5(x-3)(x-3); 2<=x<4
y=x/2-1.5; 4<=x<6
-
输入:
-
一个浮点数N
-
输出:
-
测试数据可能有多组,对于每一组数据,
输出N对应的分段函数值:f(N)。结果保留三位小数
-
样例输入:
-
1
-
样例输出:
-
1.500
分析
判断是否等于c:
fabs(x-c)<1e-8
ac代码
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#define INF 2<<20
using namespace std;
int main()
{
float x;
while(cin >> x)
{
//printf("%f\n",x-0);
if((x>0&&x<2)||(fabs(x)<1e-8))
printf("%.3f\n",2.5-x);
else if((x>2||fabs(x-2)<1e-8)&&x<4)
printf("%.3f\n",2-1.5*(x-3)*(x-3));
else if((x>4||fabs(x-4)<1e-8)&&x<6)
printf("%.3f\n",x/2-1.5);
}
return 0;
}
该博客主要解析了九度oj的一道题目1062,涉及如何判断浮点数是否等于特定值c,使用条件为fabs(x-c)<1e-8。博主提供了AC代码,通过比较x的值来应用不同的分段函数计算输出结果。代码中使用了浮点数比较的技巧以及数学公式来解决问题。

被折叠的 条评论
为什么被折叠?



