九度OJ 1062: 分段函数

来源:互联网 发布:php取字符串的某个值 编辑:程序博客网 时间:2024/06/12 20:40
题目描述:

编写程序,计算下列分段函数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套)


题目分析:


本题不难,只要把握以下两点即可:
1.用 if-else语句 正确写出分段函数。
2.用 %.3f 使输出结果保留3位小数。

源代码:

#include <stdio.h>#include <stdlib.h> double f(double x){    if(x<2)        return -x+2.5;    else if(x<4)        return 2-1.5*(x-3)*(x-3);    else        return x/2-1.5;} int main(){    double n;    while(scanf("%lf", &n) != EOF)        printf("%.3f\n", f(n));    //system("pause");    return 0;}/**************************************************************    Problem: 1062    User: superlc320    Language: C++    Result: Accepted    Time:10 ms    Memory:1020 kb****************************************************************/


0 0