九度OJ 题目1062:分段函数

来源:互联网 发布:抓取股票数据 网站 编辑:程序博客网 时间:2024/06/08 00:59


一.题目描述:
编写程序,计算下列分段函数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

二.题目分析

   水题

三.代码

#include <stdio.h>#include <stdlib.h>int main(){    double x;    while(scanf("%lf",&x)!=EOF)    {        if(x>=0&&x<2)            printf("%.3f\n",-x+2.5);        else if(x<4)            printf("%.3f\n",2-1.5*(x-3)*(x-3));        else  if(x<6)            printf("%.3f\n",x/2-1.5);    }    return 0;}


0 0
原创粉丝点击