题目描述:
编写程序,计算下列分段函数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
来源:
2001年清华大学计算机研究生机试真题(第I套)- #include <stdio.h>
- int main()
- {
- float x, y;
- while (scanf("%f", &x) != EOF)
- {
- y = 0.0;
- if (x >=0 && x < 2)
- {
- y = -x + 2.5;
- }
- else if (x >= 2 && x < 4)
- {
- y = 2 - 1.5 * (x - 3) * (x - 3);
- }
- else if (x >= 4 && x < 6)
- {
- y = x / 2 - 1.5;
- }
- printf("%.3f\n", y);
- }
- return 0;
- }
- /**************************************************************
- Problem: 1062
- User: greenrock
- Language: C++
- Result: Accepted
- Time:10 ms
- Memory:1012 kb
- ****************************************************************/
复制代码 |