九度oj 题目1062:分段函数

题目1062:分段函数

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:3874

解决:2278

题目描述:

编写程序,计算下列分段函数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
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 float f(float x){
 6     if(x >= 0 && x < 2)
 7         return -x + 2.5;
 8     else if(x < 4)
 9         return 2 - 1.5 * (x - 3) * (x - 3);
10     else
11         return x / 2 - 1.5;
12 }
13 
14 int main(){
15     float n;
16     while(cin >> n && (n >= 0) && (n < 6)){
17         printf("%.3f
", f(n));
18     }
19     return 0;
20 }
 
原文地址:https://www.cnblogs.com/qinduanyinghua/p/6483050.html